How to compress files

前端 未结 10 933
渐次进展
渐次进展 2021-02-01 06:58

I want to compress a file and a directory in C#. I found some solution in Internet but they are so complex and I couldn\'t run them in my project. Can anybody suggest me a clear

10条回答
  •  别那么骄傲
    2021-02-01 07:33

    I'm adding this answer as I've found an easier way than any of the existing answers:

    1. Install DotNetZip DLLs in your solution (easiest way is to install the package from nuget)
    2. Add a reference to the DLL.
    3. Import the namespace by adding: using Ionic.Zip;
    4. Zip your file

    Code:

    using (ZipFile zip = new ZipFile())
    {
        zip.AddFile("C:\test\test.txt");
        zip.AddFile("C:\test\test2.txt");
        zip.Save("C:\output.zip");
    }
    

    If you don't want the original folder structure mirrored in the zip file, then look at the overrides for AddFile() and AddFolder() etc.

提交回复
热议问题