Compressed stream gzipstream

Source: Internet
Author: User

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.