. Net2.0, although there is also a decompressed class, it does not seem very popular. But we can also choose not sharpziplib.
I found some code from the Internet and made some modifications. It has been tested and can be used. Decompression Operation class:
Using system;
Using system. Collections. Generic;
Using system. Web. UI. webcontrols;
Using system. Web;
Using system. text;
Using system. IO;
Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. checksums;
Namespace verycodes. Common
{
Public class ziphelper
{
///
/// Compress a single object
///
/// The file to be compressed
///
/// Compressed file
///
/// Compression level
///
/// Write size each time
Public static void zipfile (string filetozip, string zipedfile,
Int compressionlevel, int blocksize)
{
// If the file is not found, an error is returned.
If (! System. Io. file. exists (filetozip ))
{
Throw new system. Io. filenotfoundexception ("specify the file to be compressed:" +
Filetozip + "does not exist! ");
}
Using (system. Io. filestream zipfile = system. Io. file. Create (zipedfile ))
{
Using (zipoutputstream zipstream = new zipoutputstream (zipfile ))
{
Using (system. Io. filestream streamtozip =
New system. Io. filestream (filetozip, system. Io. filemode. Open,
System. Io. fileaccess. Read ))
{
String filename = filetozip. substring (filetozip. lastindexof ("\") + 1 );
Zipentry = new zipentry (filename );
Zipstream. putnextentry (zipentry );
Zipstream. setlevel (compressionlevel );
Byte [] buffer = new byte [blocksize];
Int sizeread = 0;
Try
{
Do
{
Sizeread = streamtozip. Read (buffer, 0, buffer. Length );
Zipstream. Write (buffer, 0, sizeread );
}
While (sizeread> 0 );
}
Catch (system. Exception ex)
{
Throw ex;
}
Streamtozip. Close ();
}
Zipstream. Finish ();
Zipstream. Close ();
}
Zipfile. Close ();
}
}
///
/// Compress a single object
///
/// Name of the file to be compressed
///
/// Compressed file name
Public static void zipfile (string filetozip, string zipedfile)
{
// If the file is not found, an error is returned.
If (! File. exists (filetozip ))
{
Throw new system. Io. filenotfoundexception ("specify the file to be compressed:" +
Filetozip + "does not exist! ");
}
Using (filestream FS = file. openread (filetozip ))
{
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
FS. Close ();
Using (filestream zipfile = file. Create (zipedfile ))
{
Using (zipoutputstream zipstream = new zipoutputstream (zipfile ))
{
String filename =
Filetozip. substring (filetozip. lastindexof ("\") + 1 );
Zipentry = new zipentry (filename );
Zipstream. putnextentry (zipentry );
Zipstream. setlevel (5 );
Zipstream. Write (buffer, 0, buffer. Length );
Zipstream. Finish ();
Zipstream. Close ();
}
}
}
}
///
/// Compress multi-layer Directories
///
/// Compressed directory
///
/// Compressed file stream
///
Public static void zipfiledirectory (string strdirectory, string zipedfile)
{
Using (system. Io. filestream zipfile = system. Io. file. Create (zipedfile ))
{
Using (zipoutputstream S = new zipoutputstream (zipfile ))
{
Zipsetp (strdirectory, S ,"");
}
}
}
///
/// Recursively traverse the directory
///
///
Private Static void zipsetp (string strdirectory,
Zipoutputstream S, string parentpath)
{
If (strdirectory [strdirectory. Length-1]! = Path. directoryseparatorchar)
{
Strdirectory + = path. directoryseparatorchar;
}
CRC32 CRC = new CRC32 ();
String [] filenames = directory. getfilesystementries (strdirectory );
// Traverse all files and directories
Foreach (string file in filenames)
{
// Process the file as a directory first. If this directory exists, recursively copy the file under this directory.
If (directory. exists (File ))
{
Parentpath + = file. substring (file. lastindexof ("\") + 1 );
Parentpath + = "\\";
Zipsetp (file, S, parentpath );
}
Else // otherwise, directly compress the file
{
// Open the compressed file
Using (filestream FS = file. openread (File ))
{
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
String filename = parentpath + file. substring (file. lastindexof ("\") + 1 );
Zipentry entry = new zipentry (filename );
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 );
}
}
}
}
///
/// Decompress a zip file.
///
/// ZIP file to be decompressed.
///
/// Decompress the directory of the ZIP file.
///
/// Password of the ZIP file.
///
/// Whether to overwrite existing files.
Public static void unzip (string zipedfile,
String strdirectory, string password, bool overwrite)
{
If (strdirectory = "")
Strdirectory = directory. getcurrentdirectory ();
If (! Strdirectory. endswith ("\\"))
Strdirectory = strdirectory + "\\";
Using (zipinputstream S = new zipinputstream (file. openread (zipedfile )))
{
S. Password = password;
Zipentry theentry;
While (theentry = S. getnextentry ())! = NULL)
{
String directoryname = "";
String pathtozip = "";
Pathtozip = theentry. Name;
If (pathtozip! = "")
Directoryname = path. getdirectoryname (pathtozip) + "\\";
String filename = path. getfilename (pathtozip );
Directory. createdirectory (strdirectory + directoryname );
If (filename! = "")
{
If (file. exists (strdirectory + directoryname + filename )&&
Overwrite) | (! File. exists (strdirectory +
Directoryname + filename )))
{
Using (filestream streamwriter =
File. Create (strdirectory + directoryname + 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 ();
}
}
}
}
Test:
Protected void button#click (Object sender, eventargs E)
{
Ziphelper. zipfile (@ "E: \ 02.gif", @" E: \ 02.zip ");
}
Protected void button3_click (Object sender, eventargs E)
{
Ziphelper. zipfile (@ "E: \ Asp.net Chinese manual. chm", @ "E: \ asp.net 文..zip", 5, 1024 );
}
Protected void button2_click (Object sender, eventargs E)
{
Ziphelper. Unzip ("E: \ page window dragging. Zip", "E: \ drag 2 \", "", true );
}
Protected void button4_click (Object sender, eventargs E)
{
Ziphelper. zipfiledirectory ("e :\\ drag a page window \", "e :\\ drag a page window. Zip ");
}
There will be a problem during decompression: Size mismatch: xxxxxxx there is a way, I have not carefully read, but valid: Open the source code of sharpziplib, you find zipinputstream In the ZIP folder. find the CS file and
If (flags & 8) = 0 & (INF. totalin! = Csize | INF. totalout! = Size ))
{
Throw new zipexception ("size mismatch:" + csize + ";" +
Size + "<->" + INF. totalin + ";" + INF. totalout );
} Comment out the above Code, compile it, and introduce the DLL to the project.