C # DotNetZip Compressed single, multiple files, and folders

Source: Internet
Author: User

Some projects in order to better user experience, will download the file into a compressed file, direct download, so as to avoid the click to download files. There are many ways to compress files on the Internet, there are third-party loading DLL files, this paper mainly introduces DotNetZip compression method.

DotNetZip's dll:http://download.csdn.net/detail/lilinoscar/8295255

Official website: http://dotnetzip.codeplex.com/


Solve DotNetZip compression Chinese name garbled, only need to set the encoding when instantiating: System.Text.Encoding.Default

That is: zipfile zip = new ZipFile (System.Text.Encoding.Default).

Solve DotNetZip compressed file has a multi-level directory: Zip.addfile (file, "");

AddFile plus the second parameter removes multiple layers of folders.


        #region bool SaveFile (String filePath, byte[] bytes) file save,///<summary>///File save, especially some files put into data Library, can be directly from the data binary, and then saved to the specified folder///</summary>//<param name= "FilePath" > Save file Address </param>/ <param name= "bytes" > File binary </param>//<returns></returns> public static bool Save            File (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 Determine if the folder exists///<summary>///Determine if the folder exists///</summary>  <param name= "Path" > folder address </param>//<returns></returns>      public static bool Directoryexist (string path) {if (!string.            IsNullOrEmpty (path) && directory.exists (path)) {return true;        } return false; #endregion #region Create Folder///<summary>//Create Folder///</summary>/ <param name= "Path" > File address </param>//<returns></returns> public static BOOL Direct Oryadd (string path) {if (!string. IsNullOrEmpty (path) &&!             Directory.Exists (Path)) {directory.createdirectory (path);//new folder return true;        } return false; #endregion #region Get the compressed file path///<summary>//Get compressed file path///</summary&gt        ;        <param name= "Dirpath" > Compressed file path </param>//<param name= "Filespath" > Multiple file Paths </param> <returns></returns> public static string Getcompresspath (String dirpath, list<string> Filespath) { var zippath = "";//returns the compressed file path using (ZipFile zip = new ZipFile (System.Text.Encoding.Default))//system.text.enc Oding.                    Default setting Chinese attachment name garbled, not set will appear garbled {foreach (var file in Filespath) { Zip.    AddFile (file, ""); The second parameter is empty, stating that there are no multi-tiered folders for compressed files. For example C:\test\a\b\c.doc compressed after the decompression file will appear C.doc//if changed to zip. AddFile (file), there will be multi-layer folder compression, such as C:\test\a\b\c.doc compression after extracting the file will appear Test\a\b\c.doc} Zippath = string.                Format ("{0}\\{1}.zip", Dirpath, DateTime.Now.ToString ("Yyyymmddhhmmss")); Zip.            Save (Zippath);        } return Zippath; } #endregion

Call:


                 list<string> Filespath = new list<string> (); Filespath.add ("C:/test/a.doc"); Filespath.add ("C:/test/b.doc"); Filespath.add (Server.MapPath ("~/text/files/c.doc"));//You can set the Add virtual path  var dirpath= "Server.MapPath (" ~/compress/ ")"; var filepath=getcompresspath (Dirpath,filespath);//returns the compressed file








C # DotNetZip Compressed single, multiple files, and folders

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.