Using System; Using System. IO; Using ICSharpCode. SharpZipLib. Checksums; Using ICSharpCode. SharpZipLib. Zip; Using ICSharpCode. SharpZipLib. GZip; Namespace ZipFile { /// <Summary> /// Compressed file /// </Summary> Public class ZipHelp { Public string ZipName {get; set ;} /// <Summary> /// Compressed folder /// </Summary> /// <Param name = "zipSourcePath"> path of the folder to be compressed (full path) </param> /// <Param name = "zipToFilePath"> the path saved after compression must contain a suffix such as: D: \ aa.zip (if it is null, it is saved to the same-level folder by default. The name is the source file name) </param> Public void ZipFileMain (string zipSourcePath, string zipToFilePath) { String [] filenames = Directory. GetFiles (zipSourcePath ); ZipName = zipSourcePath. Substring (zipSourcePath. LastIndexOf ("\") + 1 ); // Define the compressed directory object Crc32 crc = new Crc32 (); ZipOutputStream s = new ZipOutputStream (File. Create (zipToFilePath. Equals ("")? ZipSourcePath + ". zip": zipToFilePath )); S. SetLevel (6); // sets the compression level // Recursively compress all files and word folders in the folder AddDirToDir (crc, s, zipSourcePath ); S. Finish (); S. Close (); } /// <Summary> /// Compress a single object /// </Summary> /// <Param name = "zipSourcePath"> path of the file to be compressed (full path) </param> /// <Param name = "zipToFilePath"> path of the compressed file (if it is a null character, the file name is the source file name by default compressed to the same directory) </param> Public void ZipByFile (string zipSourcePath, string zipToFilePath) { // Define the compressed directory object Crc32 crc = new Crc32 (); String dirName = zipSourcePath. substring (zipSourcePath. lastIndexOf ("\") + 1, zipSourcePath. lastIndexOf (". ")-(zipSourcePath. lastIndexOf ("\") + 1) + ". zip "; ZipOutputStream s = new ZipOutputStream (File. Create (zipToFilePath. Equals ("")? ZipSourcePath. Substring (0, zipSourcePath. LastIndexOf ("\") + "\" + dirName: zipToFilePath )); S. SetLevel (6); // sets the compression level AddFileToDir (crc, s, zipSourcePath, 0 ); S. Finish (); S. Close (); } /// <Summary> /// Compress a single file to a specified compressed folder (called internally) /// </Summary> /// <Param name = "crc"> </param> /// <Param name = "s"> </param> /// <Param name = "file"> file path </param> Public void AddFileToDir (Crc32 crc, ZipOutputStream s, string file, int dotype) { FileStream fs = File. OpenRead (file ); Byte [] buffer = new byte [fs. Length]; Fs. Read (buffer, 0, buffer. Length ); String filename = ""; If (dotype = 0) Filename = file. Substring (file. LastIndexOf ("\") + 1 ); Else Filename = file. Substring (file. IndexOf (ZipName )); ZipEntry entry = new ZipEntry (filename ); 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 ); } /// <Summary> /// Recursive Folder level (Internal call) /// </Summary> /// <Param name = "crc"> </param> /// <Param name = "s"> </param> /// <Param name = "file"> </param> Public void AddDirToDir (Crc32 crc, ZipOutputStream s, string file) { // Add files in this folder String [] files = Directory. GetFiles (file ); Foreach (string I in files) { AddFileToDir (crc, s, I, 1 ); } // Query subfolders in this folder String [] dirs = Directory. GetDirectories (file ); Foreach (string I in dirs) { AddDirToDir (crc, s, I ); } } } } |