Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using icsharpcode. sharpziplib. checksums;
Using icsharpcode. sharpziplib. Zip. compression;
Using icsharpcode. sharpziplib. Zip. Compression. streams;
Using icsharpcode. sharpziplib. Zip;
Namespace playedit. classobj
{
// Compression class
Class classzip
{
/// <Summary>
/// Recursive folder Compression Method
/// </Summary>
/// <Param name = "foldertozip"> </param>
/// <Param name = "S"> </param>
/// <Param name = "parentfoldername"> </param>
Private Static bool zipfiledic.pdf (string foldertozip, zipoutputstream S, string parentfoldername)
{
Bool res = true;
String [] folders, filenames;
Zipentry entry = NULL;
Filestream FS = NULL;
CRC32 CRC = new CRC32 ();
Try
{
// Create the current folder
Entry = new zipentry (path. Combine (parentfoldername, path. getfilename (foldertozip) + "/"); // Add "/" to create a folder.
S. putnextentry (entry );
S. Flush ();
// First compress the file and then recursively compress the folder
Filenames = directory. getfiles (foldertozip );
Foreach (string file in filenames)
{
// Open the compressed file
FS = file. openread (File );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
Entry = new zipentry (path. Combine (parentfoldername, path. getfilename (foldertozip) + "/" + path. getfilename (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 );
}
}
Catch
{
Res = false;
}
Finally
{
If (FS! = NULL)
{
FS. Close ();
FS = NULL;
}
If (entry! = NULL)
{
Entry = NULL;
}
GC. Collect ();
GC. Collect (1 );
}
folders = directory. getdirectories (foldertozip);
foreach (string folder in folders)
{< br> If (! Zipfiledictory (folder, S, path. combine (parentfoldername, path. getfilename (foldertozip)
{< br> return false;
}< BR >}
Return res;
}
/// <Summary>
/// Compressed directory
/// </Summary>
/// <Param name = "foldertozip"> folder to be compressed, full path format </param>
/// <Param name = "zipedfile"> compressed file name, full path format </param>
/// <Returns> </returns>
Private Static bool zipfiledic.pdf (string foldertozip, string zipedfile, int level)
{
Bool res;
If (! Directory. exists (foldertozip ))
{
Return false;
}
Zipoutputstream S = new zipoutputstream (file. Create (zipedfile ));
S. setlevel (level );
Res = zipfiledic.pdf (foldertozip, S ,"");
S. Finish ();
S. Close ();
Return res;
}
/// <Summary>
/// Compressed file
/// </Summary>
/// <Param name = "filetozip"> name of the file to be compressed </param>
/// <Param name = "zipedfile"> compressed file name </param>
/// <Returns> </returns>
Private Static bool zipfile (string filetozip, string zipedfile, int level)
{
// 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! ");
}
// Filestream FS = NULL;
Filestream zipfile = NULL;
Zipoutputstream zipstream = NULL;
Zipentry = NULL;
Bool res = true;
Try
{
Zipfile = file. openread (filetozip );
Byte [] buffer = new byte [zipfile. Length];
Zipfile. Read (buffer, 0, buffer. Length );
Zipfile. Close ();
Zipfile = file. Create (zipedfile );
Zipstream = new zipoutputstream (zipfile );
Zipentry = new zipentry (path. getfilename (filetozip ));
Zipstream. putnextentry (zipentry );
Zipstream. setlevel (level );
Zipstream. Write (buffer, 0, buffer. Length );
}
Catch
{
Res = false;
}
Finally
{
If (zipentry! = NULL)
{
Zipentry = NULL;
}
If (zipstream! = NULL)
{
Zipstream. Finish ();
Zipstream. Close ();
}
If (zipfile! = NULL)
{
Zipfile. Close ();
Zipfile = NULL;
}
GC. Collect ();
GC. Collect (1 );
}
Return res;
}
/// <Summary>
/// Compress files and folders
/// </Summary>
/// <Param name = "filetozip"> file or folder to be compressed, full path format </param>
/// <Param name = "zipedfile"> compressed file name, full path format </param>
/// <Returns> </returns>
Public static bool zip (string filetozip, string zipedfile, int level)
{
If (directory. exists (filetozip ))
{
Return zipfiledic.pdf (filetozip, zipedfile, level );
}
Else if (file. exists (filetozip ))
{
Return zipfile (filetozip, zipedfile, level );
}
Else
{
Return false;
}
}
}
/// <Summary>
/// Extract class
/// </Summary>
Public class unzipclass
{
/// <Summary>
/// Extract the compressed file to the specified directory)
/// </Summary>
/// <Param name = "filetoupzip"> files to be decompressed </param>
/// <Param name = "zipedfolder"> specify the target directory for extraction </param>
Public static void unzip (string filetoupzip, string zipedfolder)
{
If (! File. exists (filetoupzip ))
{
Return;
}
If (! Directory. exists (zipedfolder ))
{
Directory. createdirectory (zipedfolder );
}
Zipinputstream S = NULL;
Zipentry theentry = NULL;
String filename;
Filestream streamwriter = NULL;
Try
{
S = new zipinputstream (file. openread (filetoupzip ));
While (theentry = S. getnextentry ())! = NULL)
{
If (theentry. Name! = String. Empty)
{
Filename = path. Combine (zipedfolder, theentry. Name );
/// Determine whether the file path is a folder
If (filename. endswith ("/") | filename. endswith ("\\"))
{
Directory. createdirectory (filename );
Continue;
}
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;
}
}
}
}
}
Finally
{
If (streamwriter! = NULL)
{
Streamwriter. Close ();
Streamwriter = NULL;
}
If (theentry! = NULL)
{
Theentry = NULL;
}
If (s! = NULL)
{
S. Close ();
S = NULL;
}
GC. Collect ();
GC. Collect (1 );
}
}
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/tcstar/archive/2007/11/08/1872832.aspx