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