I'm glad to have successfully applied for a blog in the blog Park. I 'd like to express my gratitude for writing an article.
Today, I made a project for someone else. There are not many functions, but some functions are complicated. For example, use a web page to compress and decompress files. After multiple searches, sharpziplib is used to implement the compression function.
The Code is as follows:
Using system;
Using system. IO;
Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. gzip;
Using icsharpcode. sharpziplib. Bzip2;
Using icsharpcode. sharpziplib. checksums;
Using icsharpcode. sharpziplib. Zip. compression;
Using icsharpcode. sharpziplib. Zip. Compression. streams;
/// <Summary>
/// Zip to compress all objects in a specified folder
/// </Summary>
Public class zip
{
# Region Constructor
/// <Summary>
/// Constructor
/// </Summary>
Public zip ()
{
//
// Todo: Add constructor logic here
//
}
# Endregion
# Region compression main function for compression
/// <Summary>
/// Compress the main function to implement the compression function
/// </Summary>
/// <Param name = "strfiledir"> </param>
Public void putinzipfile (string [] strfiledir)
{
String filenames = strfiledir [0];
CRC32 CRC = new CRC32 ();
Zipoutputstream S = new zipoutputstream (file. Create (ARGs [1]);
S. setlevel (6 );
Foreach (string file in directory. getfiles (filenames ))
{
Filestream FS = file. openread (File );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
String [] strname = file. Split ('\\');
Zipentry entry = new zipentry (strname [strname. Length-1]);
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 ();
}
# Endregion
}
You can compress all the files in the specified folder into A. ZIP file.
If (! Ispostback)
{
String [] fileproperties = new string [2];
String fullname = server. mappath ("Upload ");
String destpath = system. Io. Path. getdirectoryname (fullname );
// File to be compressed
Fileproperties [0] = fullname;
// Target file after compression
Fileproperties [1] = destpath + String. Format ("\\{ 02.16.zip", "XXX ");
If (file. exists (fileproperties [1])
{
File. Delete (fileproperties [1]);
}
Zip ZC = new zipclass ();
ZC. putinzipfile (fileproperties );
This. downfile. href = string. Format ("/upload/00000).zip", "XXX ");
This. downfile. Target = "_ blank ";
}
Source: http://www.cnblogs.com/zhjp11/archive/2007/05/06/736778.html