Decompress the zip and GZ files in the. NET environment.

Source: Internet
Author: User
Tags decompress zip gz file

In. NET 2.0, you can use sharpziplib, an open source DLL, to easily decompress ZIP files and GZ files.

You can download sharpziplib here.

 

The following describes the specific code:

1. Unzip the ZIP file:

Namespace: icsharpcode. sharpziplib. Zip

Private Static void uncompresszip (string file, string DIR) <br/>{< br/> try <br/>{< br/> If (! Directory. exists (DIR) <br/> directory. createdirectory (DIR); </P> <p> zipinputstream S = new zipinputstream (file. openread (File); </P> <p> zipentry theentry; <br/> while (theentry = S. getnextentry ())! = NULL) <br/>{< br/> string directoryname = path. getdirectoryname (theentry. name); <br/> string filename = path. getfilename (theentry. name); </P> <p> If (directoryname! = String. Empty) <br/> directory. createdirectory (DIR + directoryname); </P> <p> If (filename! = String. empty) <br/>{< br/> filestream streamwriter = file. create (DIR + theentry. name); </P> <p> int size = 2048; <br/> byte [] DATA = new byte [2048]; <br/> while (true) <br/>{< br/> size = S. read (data, 0, Data. length); <br/> If (size> 0) <br/>{< br/> streamwriter. write (data, 0, size); <br/>}< br/> else <br/>{< br/> break; <br/>}</P> <p> streamwriter. close (); <br/>}< br/> S. close (); <br/>}< br/> catch (exception ex) <br/>{< br/> console. writeline (ex. message); <br/>}< br/>}

Where:

File indicates the path and file name of the ZIP file.

Dir indicates the decompressed path.

 

2. Decompress GZ:

Namespace: icsharpcode. sharpziplib. Gzip

Private Static void uncompressgzip (string file, string DIR) <br/>{< br/> try <br/>{< br/> If (! Directory. exists (path. getdirectoryname (DIR) <br/> directory. createdirectory (path. getdirectoryname (DIR); </P> <p> gzipinputstream S = new gzipinputstream (file. openread (File); <br/> filestream streamwriter = file. create (DIR); </P> <p> int size = 2048; <br/> byte [] DATA = new byte [2048]; <br/> while (true) <br/>{< br/> size = S. read (data, 0, Data. length); <br/> If (size> 0) <br/>{< br/> streamwriter. write (data, 0, size); <br/>}< br/> else <br/>{< br/> break; <br/>}</P> <p> streamwriter. close (); <br/>}< br/> catch (exception ex) <br/>{< br/> console. writeline (ex. message); <br/>}< br/>} 

Where:

File indicates the path and file name of the GZ file.

Dir indicates the decompressed path and file name.

 

PS: in Windows NT, you can use the 7-zip tool to compress the file into the GZ format.

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.