Using system;
Using icsharpcode. sharpziplib;
Using icsharpcode. sharpziplib. checksums;
Using system. IO;
Using icsharpcode. sharpziplib. Zip;
Using system. collections;
/// <Summary>
/// Summary of common.
/// </Summary>
Public class commrar
{
Public commrar ()
{
//
// Todo: add the constructor logic here
//
}
//
// compressed file http://www.my400800.cn
//
// compressed file name set
// compressed file name
// / password
Public static void zipfile (string path, string destfilename)
{< br> CRC32 CRC = new CRC32 ();
string strparpath = path;
// Define
System. Io. directoryinfo mydir = new directoryinfo (PATH );
// 0-store only to 9-means best compression
If (mydir. exists = true)
{
System. Io. fileinfo [] myfileary = mydir. getfiles ();
Zipoutputstream S = new zipoutputstream (file. Create (destfilename ));
S. Password = "";
S. setlevel (6 );
// Extracts information from each file in the folder cyclically,
Foreach (fileinfo objfiles in myfileary)
{
If (objfiles. fullname. indexof (". rar")> 0) continue;
Filestream FS = file. openread (objfiles. fullname );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
Zipentry entry = new zipentry (objfiles. fullname. substring (path. Length ));
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 );
}
Foreach (directoryinfo objdire in mydir. getdirectories ())
{
Zipfolder (S, objdire. fullname, CRC, strparpath );
}
S. Finish ();
S. Close ();
}
}
Private Static void zipfolder (zipoutputstream S, string path, CRC32 CRC, string strparpath)
{
// Define
System. Io. directoryinfo mydir = new directoryinfo (PATH );
System. Io. fileinfo [] myfileary = mydir. getfiles ();
// Extracts information from each file in the folder cyclically,
Foreach (fileinfo objfiles in myfileary)
{
If (objfiles. fullname. indexof (". rar")> 0) continue;
Filestream FS = file. openread (objfiles. fullname );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
Zipentry entry = new zipentry (objfiles. fullname. substring (strparpath. Length ));
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 );
}
Foreach (directoryinfo objdire in mydir. getdirectories ())
{
Zipfolder (S, objdire. fullname, CRC, strparpath );
}
}
/// <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 ();
String strrootpath = "";
While (theentry = S. getnextentry ())! = NULL)
{
String filename = (theentry. Name );
If (filename! = "")
{
Filename = destpath + "\" + filename;
Strrootpath = path. getdirectoryname (filename );
If (! Directory. exists (strrootpath ))
{
Directory. createdirectory (strrootpath );
}
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 ();
}
}
Code download