Add reference icsharpcode. sharpziplib. dll
View plaincopy to clipboardprint?
- Using system;
- Using system. Collections. Generic;
- Using system. IO;
- Using system. Threading;
- Using icsharpcode. sharpziplib. Zip;
- Namespace lib
- {
- /// <Summary>
- /// File compression and decompression
- /// </Summary>
- Public class filecompression
- {
- /// <Summary>
- /// Constructor
- /// </Summary>
- Public filecompression ()
- {
- }
- # Region encryption and File compression
- /// <Summary>
- /// Compressed file
- /// </Summary>
- /// <Param name = "filenames"> list of files to be packaged </param>
- /// <Param name = "gzipfilename"> target file name </param>
- /// <Param name = "compressionlevel"> compression quality level (0 ~ 9) </param>
- /// <Param name = "sleeptimer"> sleep time (unit: milliseconds) </param>
- Public static void compress (list <fileinfo> filenames, string gzipfilename, int compressionlevel, int sleeptimer)
- {
- Zipoutputstream S = new zipoutputstream (file. Create (gzipfilename ));
- Try
- {
- S. setlevel (compressionlevel); // 0-store only to 9-means best compression
- Foreach (fileinfo file in filenames)
- {
- Filestream FS = NULL;
- Try
- {
- FS = file. Open (filemode. Open, fileaccess. readwrite );
- }
- Catch
- {Continue ;}
- // Method 2: Read files into the buffer in batches
- Byte [] DATA = new byte [2048];
- Int size = 2048;
- Zipentry entry = new zipentry (path. getfilename (file. Name ));
- Entry. datetime = (file. creationtime> file. lastwritetime? File. lastwritetime: file. creationtime );
- S. putnextentry (entry );
- While (true)
- {
- Size = FS. Read (data, 0, size );
- If (size <= 0) break;
- S. Write (data, 0, size );
- }
- FS. Close ();
- File. Delete ();
- Thread. Sleep (sleeptimer );
- }
- }
- Finally
- {
- S. Finish ();
- S. Close ();
- }
- }
- # Endregion
- # Region decryption and decompression of Files
- /// <Summary>
- /// Decompress the file
- /// </Summary>
- /// <Param name = "gzipfile"> compressed package file name </param>
- /// <Param name = "targetpath"> decompress the target path </param>
- Public static void decompress (string gzipfile, string targetpath)
- {
- // String directoryname = path. getdirectoryname (targetpath + "\") + "\\";
- String directoryname = targetpath;
- If (! Directory. exists (directoryname) directory. createdirectory (directoryname); // generate the decompressed directory
- String currentdirectory = directoryname;
- Byte [] DATA = new byte [2048];
- Int size = 2048;
- Zipentry theentry = NULL;
- Using (zipinputstream S = new zipinputstream (file. openread (gzipfile )))
- {
- While (theentry = S. getnextentry ())! = NULL)
- {
- If (theentry. isdirectory)
- {// The node is a directory
- If (! Directory. exists (currentdirectory + theentry. Name) directory. createdirectory (currentdirectory + theentry. Name );
- }
- Else
- {
- If (theentry. Name! = String. Empty)
- {
- // Extract the file to the specified directory
- Using (filestream streamwriter = file. Create (currentdirectory + theentry. Name ))
- {
- While (true)
- {
- Size = S. Read (data, 0, Data. Length );
- If (size <= 0) break;
- Streamwriter. Write (data, 0, size );
- }
- Streamwriter. Close ();
- }
- }
- }
- }
- S. Close ();
- }
- }
- # Endregion
- }
- }
Foreground call:
/// <Summary>
/// Compression
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void btncompress_click (Object sender, eventargs E)
{
String Path = server. mappath ("~ /Files /");
// Check whether the file exists
If (file. exists (path + "Chapter 2nd using data warehouse "))
{
Fileinfo fi1 = new fileinfo (path + "Chapter 2nd using data warehouse ");
Fileinfo fi2 = new fileinfo (path + "Chapter 3rd use data binding and datasettings ");
Fileinfo fi3 = new fileinfo (path + "Chapter 4th SQL Server xmlfunction ");
Fileinfo fi4 = new fileinfo (path + "Chapter 5th xmlprogramming example ");
List <fileinfo> filelist = new list <fileinfo> ();
Filelist. Add (fi1 );
Filelist. Add (fi2 );
Filelist. Add (fi3 );
Filelist. Add (fi4 );
// Call Method
String targetzipfilepath = server. mappath ("~ /Zipfile/") +" book.zip "; // random Extension
Lib. filecompression. Compress (filelist, targetzipfilepath, 5, 5 );
Response. Write ("the file is compressed successfully. Please check it in the zipfile folder. ");
}
Else
{
Response. Write ("the compressed file does not exist. Extract the file first. ");
}
}
/// <Summary>
/// Extract
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void btndecompress_click (Object sender, eventargs E)
{
String zipfilepath = server. mappath ("~ /Zipfile/") +" book.zip ";//
If (file. exists (zipfilepath ))
{
String targetpath = server. mappath ("~ /Files /");
Lib. filecompression. Decompress (zipfilepath, targetpath );
//// Delete the ZIP file after decompression
// File. Delete (zipfilepath );
Response. Write ("the file is decompressed successfully. Please check it in the Files folder. ");
}
Else
{
Response. Write ("the compressed file does not exist. compress the file first. ");
}
}
}