Using icsharpcode. sharpziplib. Zip must be referenced;
Call: createzipfile ("file path", "compression path ")
Private Static void createzipfile (string filespath, string zipfilepath)
{
If (! Directory. exists (filespath ))
{
MessageBox. Show ("not found" + filespath );
Return;
}
Try
{
String [] filenames = directory. getfiles (filespath );
Using (zipoutputstream S = new zipoutputstream (file. Create (zipfilepath )))
{
S. setlevel (9); // compression level 0-9
// S. Password = "123"; // zip compressed file Password
Byte [] buffer = new byte [4096]; // buffer size
Foreach (string file in filenames)
{
Zipentry entry = new zipentry (path. getfilename (File ));
Entry. datetime = datetime. now;
S. putnextentry (entry );
Using (filestream FS = file. openread (File ))
{
Int sourcebytes;
Do
{
Sourcebytes = FS. Read (buffer, 0, buffer. Length );
S. Write (buffer, 0, sourcebytes );
} While (sourcebytes> 0 );
}
}
S. Finish ();
S. Close ();
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message. tostring ());
}
}
Call: unzipfile ("compression path", "file path ")
Private Static void unzipfile (string zipfilepath, string unzipfilepath)
{
If (! File. exists (zipfilepath ))
{
MessageBox. Show (zipfilepath + "does not exist! ");
Return;
}
Using (zipinputstream S = new zipinputstream (file. openread (zipfilepath )))
{
zipentry theentry;
string directoryname = path. getdirectoryname (unzipfilepath);
// create directory
If (directoryname. length> 0)
{< br> directory. createdirectory (directoryname);
}
while (theentry = S. getnextentry ())! = NULL)
{< br> // string filename = path. getfilename (theentry. name);
string filename = path. combine (unzipfilepath, theentry. name);
If (filename! = String. Empty)
{
Using (filestream streamwriter = file. Create (filename ))
{
Int size = 2048;
Byte [] DATA = new byte [2048];
While (true)
{
Size = S. Read (data, 0, Data. Length );
If (size> 0)
{
Streamwriter. Write (data, 0, size );
}
Else
{
Break;
}
}
Streamwriter. Close ();
}
}
}
S. Close ();
}
}
// -- Supplement
C # Use icsharpcode sharpziplib for compression and decompression to Compress folders
ExtractCode:
Using system;
Using system. Collections. Generic;
Using system. text;
Using icsharpcode. sharpziplib. Zip;
Using system. IO;
Namespace drmencryption
{
///; Summary;
/// Unzip class is used to extract a zip file.
///;/Summary;
Public class unzipdir
{
// Decompress the file name and path and the path before compression.
Public static Boolean unzipfile (string filetozip, string zipedfile)
{
Try
{
Fastzip = new fastzip ();
Fastzip. extractzip (filetozip, zipedfile ,"");
Return true;
}
Catch {
Return false;
}
}
}
}
Compressed file code:
Using system;
Using system. IO;
Using icsharpcode. sharpziplib. checksums;
Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. gzip;
Namespace drmencryption
{
Public class zipclass
{
Public static Boolean zipfile (string filetozip, string zipedfile)
{
Try
{
Fastzip = new fastzip ();
Bool recurse = true;
// The compressed file name, compressed directory, and Recursion
Fastzip. createzip (filetozip, zipedfile, recurse ,"");
Return true;
}
Catch {return false ;}
}
}
}
The return method is used to determine whether the decompression is successful on another page;
Decompress: unzipdir. unzipfile (file name, PATH );
Compression: zipclass. zipfile (file name, PATH );
Http://hi.baidu.com/supplyair/blog/item/9f75072c55c498eb8a1399ad.html
Decompress the Code:
Using system;
Using system. Collections. Generic;
Using system. text;
Using icsharpcode. sharpziplib. Zip;
Using system. IO;
Namespace drmencryption
{
///; Summary;
/// Unzip class is used to extract a zip file.
///;/Summary;
Public class unzipdir
{
// Decompress the file name and path and the path before compression.
Public static Boolean unzipfile (string filetozip, string zipedfile)
{
Try
{
Fastzip = new fastzip ();
Fastzip. extractzip (filetozip, zipedfile ,"");
Return true;
}
Catch {
Return false;
}
}
}
}
Compressed file code:
Using system;
Using system. IO;
Using icsharpcode. sharpziplib. checksums;
Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. gzip;
Namespace drmencryption
{
Public class zipclass
{
Public static Boolean zipfile (string filetozip, string zipedfile)
{
Try
{
Fastzip = new fastzip ();
Bool recurse = true;
// The compressed file name, compressed directory, and Recursion
Fastzip. createzip (filetozip, zipedfile, recurse ,"");
Return true;
}
Catch {return false ;}
}
}
}
The return method is used to determine whether the decompression is successful on another page;
Decompress: unzipdir. unzipfile (file name, PATH );
Compression: zipclass. zipfile (file name, PATH );
Http://hi.baidu.com/supplyair/blog/item/9f75072c55c498eb8a1399ad.html