Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using icsharpcode. sharpziplib. Zip; // open-source tool, which can be downloaded for free ::
// Http://files.cnblogs.com/xiaowei0705/SharpZipLib_0860_Bin.zip
Using system. IO;
Namespace package
{
Class class1
{
# Region pressurization and decompression method
/// <Summary>
/// Function: compress the file (only files in the directory at the lower level of the folder are temporarily compressed, and the folder and its sub-level are ignored)
/// </Summary>
/// <Param name = "dirpath"> compressed folder path </param>
/// <Param name = "zipfilepath"> Generate the path of the compressed file. If it is null, the directory is the same as that of the compressed folder by default. The name is: folder name. Zip </param>
/// <Param name = "Err"> error message </param>
/// <Returns> whether compression is successful </returns>
Public bool zipfile (string dirpath, string zipfilepath, out string ERR)
{
Err = "";
If (dirpath = string. Empty)
{
Err = "the folder to be compressed cannot be blank! ";
Return false;
}
If (! Directory. exists (dirpath ))
{
Err = "the folder to be compressed does not exist! ";
Return false;
}
// When the compressed file name is empty, use the folder name. Zip
If (zipfilepath = string. Empty)
{
If (dirpath. endswith ("\\"))
{
Dirpath = dirpath. substring (0, dirpath. Length-1 );
}
Zipfilepath = dirpath + ". Zip ";
}
Try
{
String [] filenames = directory. getfiles (dirpath );
Using (zipoutputstream S = new zipoutputstream (file. Create (zipfilepath )))
{
S. setlevel (9 );
Byte [] buffer = new byte [4096];
Foreach (string file in filenames)
{
Zipentry entry = new zipentry (path. getfilename (File ));
Entry. datetime = datetime. now;
S. putnextentry (entry );
Using (filestream FS = file. openread (File ))
{
Int sourcebytes;
Do
{
Sourcebytes = FS. Read (buffer, 0, buffer. Length );
S. Write (buffer, 0, sourcebytes );
} While (sourcebytes> 0 );
}
}
S. Finish ();
S. Close ();
}
}
Catch (exception ex)
{
Err = ex. message;
Return false;
}
Return true;
}
/// <Summary>
/// Function: Decompress the ZIP file.
/// </Summary>
/// <Param name = "zipfilepath"> compressed file path </param>
/// <Param name = "unzipdir"> decompress the file storage path. If it is null, folders with the same name as compressed files are in the same directory as compressed files by default. </param>
/// <Param name = "Err"> error message </param>
/// <Returns> whether the decompression is successful </returns>
Public bool unzipfile (string zipfilepath, string unzipdir, out string ERR)
{
Err = "";
If (zipfilepath = string. Empty)
{
Err = "the compressed file cannot be blank! ";
Return false;
}
If (! File. exists (zipfilepath ))
{
Err = "the compressed file does not exist! ";
Return false;
}
// When the decompressed folder is empty, the folder with the same name as the compressed file is in the same level as the compressed file by default.
If (unzipdir = string. Empty)
Unzipdir = zipfilepath. Replace (path. getfilename (zipfilepath), path. getfilenamewithoutextension (zipfilepath ));
If (! Unzipdir. endswith ("\\"))
Unzipdir + = "\\";
If (! Directory. exists (unzipdir ))
Directory. createdirectory (unzipdir );
Try
{
Using (zipinputstream S = new zipinputstream (file. openread (zipfilepath )))
{
Zipentry theentry;
While (theentry = S. getnextentry ())! = NULL)
{
String directoryname = path. getdirectoryname (theentry. Name );
String filename = path. getfilename (theentry. Name );
If (directoryname. length> 0)
{
Directory. createdirectory (unzipdir + directoryname );
}
If (! Directoryname. endswith ("\\"))
Directoryname + = "\\";
If (filename! = String. Empty)
{
Using (filestream streamwriter = file. Create (unzipdir + theentry. Name ))
{
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;
}
}
}
}
} // While
}
}
Catch (exception ex)
{
Err = ex. message;
Return false;
}
Return true;
} // Decompress the package
# Endregion
}
}