How to Use sharpziplib for zip compression and decompression in C)

Source: Internet
Author: User
From: http://www.cftea.com/c/2008/04/A1FQ34RYSYNLFT47.asp

Sharpziplib is a free component that can be used to compress and decompress files in multiple formats such as zip.

  • Download sharpziplib 0.85.4 locally;
  • Download the source file and example of sharpziplib 0.85.4 locally;
  • Download the sharpziplib 0.85.4 help locally.

Or you can go to the official website to download the latest version: http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

Fortunately, the fastzip class is provided in this version to facilitate compression and decompression.

Icsharpcode. sharpziplib. Zip. fastzip = new icsharpcode. sharpziplib. Zip. fastzip ();
Zip. createzip ("C: \ foo.zip", "C: \ folder to be compressed", true, ""); // The third parameter indicates whether to compress the subfolders.
Zip. extractzip ("C: \ foo.zip", "C: \ compressed folder ","");

What if we only want to compress one or some files in the directory, or we only want to decompress one or some files in the file? The last parameter must be used as a regular expression of the string type. For example, to only compress the INI file, use "^. * (. INI) $ ". For more information about regular expressions, see Introduction to regular expressions (4 ).

In addition, we provide an example of zipinputstream for decompression. In this example, the compressed file is a zip file containing only one TXT file, which is only used as an example.

Using icsharpcode. sharpziplib. Zip;
Using system. IO;

//......

Zipinputstream zipstream = new zipinputstream (file. Open ("C: \ foo.zip", filemode. Open ));

Zipentry entry = zipstream. getnextentry ();
While (entry! = NULL)
{
If (! Entry. isfile)
{
Continue;
}

Filestream writer = file. Create ("C: \ foo.txt"); // decompressed File

Int buffersize = 2048; // buffer size
Int readcount = 0; // read the actual bytes of the buffer.
Byte [] buffer = new byte [buffersize];
Readcount = zipstream. Read (buffer, 0, buffersize );
While (readcount> 0)
{
Writer. Write (buffer, 0, readcount );
Readcount = zipstream. Read (buffer, 0, buffersize );
}

Writer. Close ();
Writer. Dispose ();

Entry = zipstream. getnextentry ();
}

Zipstream. Close ();
Zipstream. Dispose ();
Related Article

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.