C # DotNetZip: compressing a single file, multiple files, and folders
For better user experience, some projects will make the downloaded files into a compressed file and download them directly, so that you do not need to download the files one by one. There are many methods for compressing files on the Internet, as well as third-party package DLL files. This article mainly introduces the DotNetZip compression method.
To solve Chinese name garbled characters in DotNetZip compression, you only need to set the Encoding during instantiation: System. Text. Encoding. Default
That is, ZipFile zip = new ZipFile (System. Text. Encoding. Default ).
After DotNetZip is compressed, there are multiple directories: zip. AddFile (file ,);
Add the second parameter to AddFile to remove multiple layers of folders.
# Region bool SaveFile (string filePath, byte [] bytes) File Save ,////// File storage, especially when some files are stored in the database, binary data can be taken directly from the data and then saved to the specified folder //////Save the file address ///File binary // www.bkjia.com
Public static bool SaveFile (string filePath, byte [] bytes) {bool result = true; try {using (var fileStream = new FileStream (filePath, FileMode. create) {fileStream. write (bytes, 0, bytes. length) ;}} catch (Exception) {result = false;} return result ;}# endregion # region determines whether a folder exists ////// Determine whether a folder exists //////Folder address ///
Public static bool directoryExist (string path) {if (! String. IsNullOrEmpty (path) & Directory. Exists (path) {return true;} return false ;}# endregion # region create a folder ////// Create a folder //////File address ///
Public static bool directoryAdd (string path) {if (! String. IsNullOrEmpty (path )&&! Directory. Exists (path) {Directory. CreateDirectory (path); // create a folder return true;} return false;} # endregion # region obtain the compressed file path ////// Obtain the compressed file path //////Compressed file path ///Multiple File paths ///
Public static string GetCompressPath (string dirPath, List
FilesPath) {var zipPath =; // return the compressed file path using (ZipFile zip = new ZipFile (System. text. encoding. default) // System. text. encoding. default: Garbled characters are set for Chinese attachments. If this parameter is not set, garbled characters are displayed. {foreach (var file in filesPath) {zip. addFile (file,); // The second parameter is null, indicating that the compressed file does not have multi-level folders. For example, C: estac.doc. After compression, the decompressed file will show c.doc // if it is changed to zip. addFile (file); multi-layer folder compression occurs. For example, testac.doc} zipPath = string is displayed after C: estac.doc is compressed and decompressed. format ({0} \ {1).zip, dirPath, DateTime. now. toString (yyyyMMddHHmmss); zip. save (zipPath) ;}return zipPath ;}# endregion
Call:
List
FilesPath = new List
(); FilesPath. add ("C:/test/a.doc"); filesPath. add ("C:/test/B .doc"); // filesPath. add (Server. mapPath (~ /Text/Files/c.doc); // you can add the virtual path var dirPath = Server. MapPath (~ /Compress/); var filePath = GetCompressPath (dirPath, filesPath); // return the compressed file