Use gzipstream to compress and decompress files

Source: Internet
Author: User
Recently, we developed a small program that uses gzipstream in. Net to compress and decompress the GZIP file. Gzipstream is easy to use under system. Io. compression. Although gzipstream is a lower-level class of the stream class, it is equivalent to only one converter. Convert data into compressed or decompressed data between two streams. The following is a simple example: static void main (string [] ARGs) {string inputfilename = @ "testfile/test.doc"; string outputfilename = @ "testfile/test.doc.gz "; // input/output data stream filestream inputstream = new filestream (inputfilename, filemode. open, fileaccess. read); filestream outputstream = new filestream (outputfilename, filemode. create, fileaccess. write); // read data to an array of byte [] buffer = new byte [inputstream. length]; inputs Tream. read (buffer, 0, buffer. length); gzipstream compressionstream = new gzipstream (outputstream, compressionmode. compress); // write the data in the array to the output data stream compressionstream through gzipstream. write (buffer, 0, buffer. length); compressionstream. close (); inputstream. close (); outputfilename. close (); console. writeline ("finished"); console. readline ();} the preceding example can meet the basic compression requirements, but it has a major drawback, that is, all files must be read into the memory (that is, the byte array) before compression can be performed.. When a large file is compressed, the system performance may be greatly affected, or even cause a system crash. So I improved him to read and compress only one part of the file at a time: static void main (string [] ARGs) {string inputfilename = @ "testfile/test.doc "; string outputfilename = @ "testfile/test.doc.gz"; filestream inputstream = new filestream (inputfilename, filemode. open, fileaccess. read); filestream outputstream = new filestream (outputfilename, filemode. create, fileaccess. write); // determines the size of a data stream read at a time. The size is 8 KB int buffersize = 8192; int bytesread = 0; B Yte [] buffer = new byte [buffersize]; gzipstream compressionstream = new gzipstream (outputstream, compressionmode. compress); // bytesread returns the amount of data read each time. If it is equal to 0, no data is available. // you can read while (bytesread = inputstream. read (buffer, 0, buffersize)> 0) {// write the data read from the array to the output data stream compressionstream through gzipstream. write (buffer, 0, bytesread);} compressionstream. close (); inputstream. close (); outputstream. close (); console. W Riteline ("finished"); console. Readline ();} Now you can solve the performance problem you just mentioned. The decompressed file is basically the same as the compressed file, except that gzipstream needs to read data from the compressed file and decompress it, and then write the decompressed data to another file, so this time gzipstream is reading. Let's look at an example: static void main (string [] ARGs) {string inputfilename = @ "testfile/test.doc.gz "; string outputfilename = @ "testfile/test_unzipped.doc"; filestream inputstream = new filestream (inputfilename, filemode. open, fileaccess. read); filestream outputstream = new filestream (outputfilename, filemode. create, fileaccess. Write); int buffersize = 8192; int bytesread = 0; byte [] buffer = new byte [buffersize]; gzipstream decompressionstream = new gzipstream (inputstream, compressionmode. decompress); // decompress the compressed data through gzipstream and then read it out. // The data read is stored in the array while (bytesread = decompressionstream. read (buffer, 0, buffersize)> 0) {// write the decompressed data to the output data stream outputstream. write (buffer, 0, bytesread);} decompressionstream. close (); input Stream. close (); outputstream. close (); console. writeline ("finished"); console. readline ();} I optimized the program I just wrote and created a relatively easy-to-use gzip tool-gziptool. Below are several methods supported by this tool: // compresses the specified file, tracks the compression progress, and sets the buffer size gziptool. compress (string inputfilename, progresshandler handler, int buffersize); example: static void main (string [] ARGs) {string inputfilename = @ "testfile/test.doc"; // compress the specified file, shows the progress and sets the size of the compressed data at a time. compress (inputfilename, new G Ziptool. progresshandler (_ progress), 20480); console. writeline ("finished"); console. readline ();} // display progress data Private Static void _ progress (long totalbytesprocessed, long totalbytes) {console. writeline (double) totalbytesprocessed/(double) totalbytes ). tostring ("p");} gziptool also supports directly compressing data into data streams, which can be used in environments without a file system, such as network transmission. // Compress the input data stream and return the compressed data packet to memorystream gziptool in a memorystream. compress (Stream inputstream) gziptool also supports progress tracking when extracting files. // decompressing a specified file with a specified name, tracking the compression progress, and setting the buffer size gziptool. decompress (string gzipfilename, string outputfilename, progresshandler handler, int buffersize) gziptool also supports reading the description of gzip files, including the original file size. // read the description of the specified GZIP file to gzipfileinfo gziptool in a gzipfileinfo structure. getfileinfo (string gzipfilename) Example: static void main (string [] ARGs) {string inputfilename = @ "testfile/test.doc.gz"; gzipfileinfo fileinfo = gziptool. getfileinfo (inputfilename); console. writeline ("GZIP file name: {0}", inputfilename); // outputs the size of the original file console. writeline ("original file size: {0}", fileinfo. originalfilesize); console. writeline ("finished"); console. readline ();}

 

Use gzipstream to compress and decompress files

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.