When looking at DNN, we found a cool feature: the ability to compress Zip files and generate Zip file packages through the IE browser. After carefully reading the program, we found that it is the compression and decompression function of the content in the SharpZipLib. dll class library called. I checked SharpZipLib on the Internet and found that it is open source, there are under the http://www.icsharpcode.net website. The source files and call demos for SharpZipLib on the website, including the help documentation, are all E files. (I really don't know which company in China is doing. net open source. I really want to see excellent open source projects made in China)
Implementation of decompression in SharpZipLib (DEMO code ):
Using System;
Using System. Text;
Using System. Collections;
Using System. IO;
Using System. Diagnostics;
Using System. Runtime. Serialization. Formatters. Binary;
Using System. Data;
Using ICSharpCode. SharpZipLib. BZip2;
Using ICSharpCode. SharpZipLib. Zip;
Using ICSharpCode. SharpZipLib. Zip. Compression;
Using ICSharpCode. SharpZipLib. Zip. Compression. Streams;
Using ICSharpCode. SharpZipLib. GZip;
Class MainClass
{
// Enter the Zip file name to be decompressed under the control command line
Public static void Main (string [] args)
{
// Create an object for reading the Zip file
ZipInputStream s = new ZipInputStream (File. OpenRead (args [0]);
// Every file in the Zip file
ZipEntry theEntry;
// Read every file in the Zip file cyclically
While (theEntry = s. GetNextEntry ())! = Null ){
Console. WriteLine (theEntry. Name );
String directoryName = Path. GetDirectoryName (theEntry. Name );
String fileName = Path. GetFileName (theEntry. Name );
// Create directory
Directory. CreateDirectory (directoryName );
If (fileName! = String. Empty ){
// Decompress the file
FileStream streamWriter = File. Create (theEntry. Name );
Int size = 2048;
Byte [] data = new byte [2048];
While (true ){
// Write data
Size = s. Read (data, 0, data. Length );
If (size> 0 ){
StreamWriter. Write (data, 0, size );
} Else {
Break;
}
}
StreamWriter. Close ();
}
}
S. Close ();
}
}
Implementation of compression in SharpZipLib: using System;
Using System. IO;
Using ICSharpCode. SharpZipLib. Checksums;
Using ICSharpCode. SharpZipLib. Zip;
Using ICSharpCode. SharpZipLib. GZip;
Class MainClass
{
// Enter the folder to be compressed and the name of the Zip file to be created
Public static void Main (string [] args)
{
String [] filenames = Directory. GetFiles (args [0]);
Crc32 crc = new Crc32 ();
// Create an output Zip file object
ZipOutputStream s = new ZipOutputStream (File. Create (args [1]);
// Set the compression level
S. SetLevel (6); // 0-store only to 9-means best compression
// Read the file to be compressed cyclically and write it into the compressed package
Foreach (string file in filenames ){
FileStream fs = File. OpenRead (file );
Byte [] buffer = new byte [fs. Length];
Fs. Read (buffer, 0, buffer. Length );
ZipEntry entry = new ZipEntry (file );
Entry. DateTime = DateTime. Now;
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 ();
}
}
Class should have other features, not yet available. The specific implementation of SharpZipLib has not been available yet. Please try the simplest function first. If you are interested, please download it. By default, the demo program is run in the console (it seems that there are other environments running, but I didn't try it). I developed a WEB application, I hope it will be useful to you.
Download the demo program on the WEB>
More>