By referencing a DLL (icsharpcode. dll), you can implement the described functions...
1. compressed files
Using system;
Using icsharpcode. sharpziplib;
Using icsharpcode. sharpziplib. checksums;
Using system. IO;
Using icsharpcode. sharpziplib. Zip;
Using system. collections;
Namespace wsupfiles
{
/// <Summary>
/// Summary of common.
/// </Summary>
Public class common
{
Public common ()
{
//
// Todo: add the constructor logic here
//
}
/// <Summary>
/// Compressed file
/// </Summary>
/// <Param name = "sourcefilenames"> set of compressed file names </param>
/// <Param name = "destfilename"> name of the compressed file </param>
/// <Param name = "password"> password </param>
Public static void zipfile (string path, string destfilename)
{
CRC32 CRC = new CRC32 ();
Zipoutputstream S = new zipoutputstream (file. Create (destfilename ));
S. Password = "";
S. setlevel (6); // 0-store only to 9-means best compression
// Define
System. Io. directoryinfo mydir = new directoryinfo (PATH );
If (mydir. exists = true)
{
System. Io. fileinfo [] myfileary = mydir. getfiles ();
// Extracts information from each file in the folder cyclically,
Foreach (fileinfo objfiles in myfileary)
{
Filestream FS = file. openread (objfiles. fullname );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
Zipentry entry = new zipentry (objfiles. fullname );
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 ();
}
}
}
}
To encrypt the compressed file, S. Password = "AAA"; AAA is the password.
Ii. decompress the file
/// <Summary>
/// Decompress the file
/// </Summary>
/// <Param name = "sourcefilename"> name of the extracted file </param>
/// <Param name = "destpath"> decompressed file directory </param>
/// <Param name = "password"> password </param>
Public static void unzipfile (string sourcefilename, string destpath, string filetype)
{
Zipinputstream S = new zipinputstream (file. openread (sourcefilename ));
Zipentry theentry;
Arraylist Al = new arraylist ();
While (theentry = S. getnextentry ())! = NULL)
{
String filename = path. getfilename (theentry. Name );
If (filename! = "")
{
Filename = destpath + "\" + filename;
If (! Directory. exists (destpath ))
{
Directory. createdirectory (destpath );
}
Filestream streamwriter = file. Create (filename );
Int size = 2048;
Byte [] DATA = new byte [2048];
S. Password = "";
While (true)
{
Size = S. Read (data, 0, Data. Length );
If (size> 0)
{
Streamwriter. Write (data, 0, size );
}
Else
{
Break;
}
}
Streamwriter. Close ();
}
}
S. Close ();
}
Note:ProgramThe compressed files to be decompressed by tools on the system will have a considerable number of paths, because they retain the original absolute path during compression, however, the compressed file only contains the compressed target file. The file extracted by the program is the relative file path.
Turn: http://blog.csdn.net/jinru2560/archive/2006/01/12/577493.aspx