Compression package production is also a feature that needs to be used in many projects. For example, there are a large number of files (suppose there are 10,000) that need to be uploaded, and uploading one or more of them seems not reliable (dead, when will it be transmitted?), at this time, we can create a compressed package zip, directly pass the file to the server side, and then decompress it in the server directory to release the files inside.
Here we choose this class library to achieve our needs.
Two compression algorithm implementations are provided, namely BZIP2 (long compression time and high compression rate) and GZIP (high compression efficiency and low compression rate).
First define an enumeration to indicate which compression algorithm the program is.
/// <summary> /// Compress enumeration/// </summary> public enum ZipEnum { //The compression time is long and the compression rate is highBZIP, //High compression efficiency and low compression rateGZIP }
Compression of a single file:
#region Make compression packages (single file compression)/// <summary> /// Make a compression package (single file compression)/// </summary> /// <param name="sourceFileName">original file</param>/// <param name="zipFileName">Compressed file</param>/// <param name="zipEnum">Compression algorithm enumeration</param>/// <returns>Compress success flag</returns>public static bool ZipFile(string srcFileName, string zipFileName, ZipEnum zipEnum) { bool flag = true; try { switch (zipEnum) { case : FileStream inStream = (srcFileName); FileStream outStream = (zipFileName, ); //Article true means that after the compression is completed, both inStream and outStream connections are released(inStream, outStream, true, ); (); (); break; case : FileStream srcFile = (srcFileName); GZipOutputStream zipFile = newGZipOutputStream((zipFileName, )); byte[] fileData = new byte[]; (fileData, , (int)); (fileData, , ); (); (); break; default: break; } } catch { flag = false; } return flag; }#endregion
Decompression of a single file:
#region decompression package (single file decompression)/// <summary> /// Unzip the package (single file decompression)/// </summary> /// <param name="zipFileName">Compressed file</param>/// <param name="unzipFileName">Decompress the file</param>/// <param name="zipEnum">Compression algorithm enumeration</param>/// <returns>Compress success flag</returns>public static bool UnZipFile(string zipFileName, stringunzipFileName, ZipEnum zipEnum) { bool flag = true; try { switch (zipEnum) { case : FileStream inStream = (zipFileName); FileStream outStream = (unzipFileName, ); (inStream, outStream, true); break; case : GZipInputStream zipFile = newGZipInputStream((zipFileName)); FileStream destFile = (unzipFileName, ); int bufferSize = * ; byte[] fileData = new byte[bufferSize]; while (bufferSize > ) { bufferSize = (fileData, , bufferSize); (fileData, , bufferSize); } (); (); break; default: break; } } catch { flag = false; } return flag; } #endregion
The above two methods can be used directly after referring to the dll.
After reading this, I believe readers have questions. What if I want to compress multiple files into 1 zip package? Can you even encrypt files? Comment on the zip package?
OK, I will continue to post two methods here, both of which have been tested and available.
Make a compression package:
#region Creates a compressed package (multiple files into one compressed package, supporting encryption and annotation)/// <summary> /// Make a compressed package (multiple files into one compressed package, supporting encryption and annotation)/// </summary> /// <param name="topDirectoryName">Compressed file directory</param>/// <param name="zipedFileName">Compressed package file name</param>/// <param name="compressionLevel">Compression Level -</param>/// <param name="password">Password</param>/// <param name="comment">Comment</param>public static void ZipFiles(string topDirectoryName, string zipedFileName, intcompresssionLevel, string password, string comment) { using (ZipOutputStream zos = newZipOutputStream((zipedFileName, ))) { if (compresssionLevel != ) { (compresssionLevel);//Set compression level} if (!(password)) { = password;//Set the zip package encryption password} if (!(comment)) { (comment);//Set the comments of the zip package} //Loop setting all *.jpg files in the directory (support subdirectory search)foreach (string file (topDirectoryName, "*.jpg", )) { if ((file)) { FileInfo item = new FileInfo(file); FileStream fs = (); byte[] buffer = new byte[]; (buffer, , ); ZipEntry entry = new ZipEntry(); (entry); (buffer, , ); } } } } #endregion
Unzip the package:
#region Unzip the package (decompress the package to the specified directory)/// <summary> /// Decompress the package (decompress the compressed package to the specified directory)/// </summary> /// <param name="zipedFileName">Compression Package Name</param>/// <param name="unZipDirectory">Decompress the directory</param>/// <param name="password">Password</param>public static void UnZipFiles(string zipedFileName, string unZipDirectory, stringpassword) { using (ZipInputStream zis = newZipInputStream((zipedFileName, ))) { if (!(password)) { = password;//If there is an encrypted file, you can set the password to decompress} ZipEntry zipEntry; while ((zipEntry = ()) != null) { string directoryName = (unZipDirectory); string pathName = (); string fileName = (); pathName = (".", "$"); directoryName += "\\" + pathName; if (!(directoryName)) { (directoryName); } if (!(fileName)) { FileStream fs = ((directoryName, fileName)); int size = ; byte[] bytes = new byte[]; while (true) { size = (bytes, , ); if (size > ) { (bytes, , size); } else { break; } } (); } } } } #endregion
When calling, we can write this:
(@"E:\\test\\", "E:\\", 1, "admin", "this is admin's comment.");//Make a compression package("E:\\", "E:\\guwei4037\\", "admin");//Unzip the package
The above content is a related introduction to the generation of Zip compressed package code based on C#. I hope it will be helpful to everyone!