System. Io. CompressionYes. NET 2.0Compression-related namespaces,However, it is not very convenient to use. Use3FangkuZiplibYou can easily perform compression operations.
Slave[1]
Download the dynamic library, and then in the projectAdd referenceIcsharpcode. sharpziplib. dllAdd.
InCodeCreateZipThe Package example is as follows (fromZiplib sample code)
Using
Icsharpcode. sharpziplib. checksums;
Using
Icsharpcode. sharpziplib. Zip;
Using
Icsharpcode. sharpziplib. gzip;
Compress all files in the specified directory to oneZipBag
Private
VoidZipthereports ()
{
String
Year = system.Datetime. Today. year. tostring ();
String
Month = system.Datetime. Today. Month. tostring ();
String
Days = system.Datetime. Today. Day. tostring ();
String[]
Filenames =Directory. Getfiles (curdir );
String
Zipfilename ="Rplan Report"+
Year + month + days +". Zip";
String
Zipabspath =This. Curdir + zipfilename;
Ziprelativepath = zipfilename;
CRC32
CRC = New CRC32 ();
zipoutputstream
S = New zipoutputstream ( file . create (zipabspath); // specify zip file absolute path, including file names
S. setlevel (6 );// 0-store only to 9-means best compression
Foreach
(StringFileIn
Filenames)
{
Filestream
FS =File. Openread (File );//The absolute path of the file, including the file name.
Byte [] Buffer
= New Byte [Fs. Length];
FS. Read (buffer, 0,
Buffer. Length );
Zipentry
Entry = New Zipentry (Extractfilename (File ));
// Here Zipentry The parameter must be a relative path name, indicating that Zip Relative Path in the document
Entry. datetime =Datetime. Now;
// SET SIZE
And the CRC, because the information
//
About the size and CRC shocould be stored in the header
// If
It is not set it is automatically written in the footer.
//
(In this case size = CRC =-1 in the header)
//
Some zip programs have problems with zip files that don't store
//
The size and CRC in the header.
Entry. size = FS. length;
FS. Close ();
CRC. Reset ();
CRC. Update (buffer );
Entry. CRC = CRC. value;
S. putnextentry (entry );
S. Write (buffer, 0,
Buffer. Length );
}
S. Finish ();
S. Close ();
}
// E.g. Convert C: \ temp \ test.xls to test.xls
Private String Extractfilename(StringFilepath)
{
Int
Index1 = filepath. lastindexof ("\\");
String
Filename =
Filepath. substring (index1 + 1 );
Return
Filename;
}
Reference
1.Ziplib
Http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
A port of zlib library to C #. Its license allows developers
To include this library in your cial, closed-source applications.
2. system. Io. Compression namespace
Http://msdn2.microsoft.com/en-us/library/system.io.compression.aspx
Deflatestream class
Http://msdn2.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx
3.Compression
Application sample
Http://msdn2.microsoft.com/en-us/library/ywf6dxhx.aspx
This sample demonstrates compression capabilities available
In the. NET Framework. It builds a Windows Forms Application that employs
Gzipstream and deflatestream types to compress and decompress files. The sample
Also introduces several types that are new in the. NET Framework Version 2.0.
4. Using the zip classes in the J # class libraries to compress
Files and data with C #
Http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/default.aspx