1> File compression process:
1. Create read stream file. openread ()
2. Create a write stream file. openwrite ();
3. Create a compressed stream new gzipstream (). Write the stream as the parameter and.
4. read part of data each time by reading the stream and write data by compressing the stream.
2> file decompression process
1. Create a read stream: file. openread ()
2. Create a compressed stream: New gzipstream (); read the stream as a parameter
3. Create a write stream file. openwrite ();
4. Each time data is read through a compressed stream, data is written into the stream.
Example of File compression:
// 1. Create a stream for reading text files
Using (filestream fsread = file. openread ("a.txt "))
{
// 2. Create a stream to write text files
Using (filestream fswrite = file. openwrite ("aa.txt "))
{
// 3. Create a compressed stream
Using (gzipstrzipstream zipstream = EAM zipstream = new gzipstream (fswrite, compressionmode. Compress ))
{
// 4. Read 1024 bytes each time
Byte [] byts = new byte [1024];
Int Len = 0;
While (LEN = fsread. Read (byts, 0, byts. Length)> 0)
{
// Write a file through a compressed stream
Zipstream. Write (byts, 0, Len );
}
}
}
}
Console. writeline ("OK ");
Console. readkey ();
File solution example:
Using (filestream fsread = file. openread ("yasuo.txt "))
{
Using (gzipstream = new gzipstream (fsread, compressionmode. Decompress ))
{
Using (filestream fswrite = file. openwrite ("jieya.txt "))
{
Byte [] byts = new byte [1024];
Int Len = 0;
While (LEN = gzipstream. Read (byts, 0, byts. Length)> 0)
{
Fswrite. Write (byts, 0, Len );
}
}
}
}
Console. writeline ("OK ");
Console. readkey ();