How to compress a directory into a zip file progra

2019-01-23 05:07发布

I want to compress an entire directory which can have any number of subdirectories into a single ZIP file.

I am able to compress a single file into a zip file programmatically.

To compress an entire directory, i can think of a recursive program that walks through each subdirectory and compresses it.

But Is there any simple way to compress the entire folder using the similar code, without having to write any recursive functions?

标签: c# .net zip gzip
4条回答
该账号已被封号
2楼-- · 2019-01-23 05:27

Using DotNetZip, there's an AddDirectory() method on the ZipFile class that does what you want:

using (var zip = new Ionic.Zip.ZipFile())
{
    zip.AddDirectory("DirectoryOnDisk", "rootInZipFile");
    zip.Save("MyFile.zip");
}

This example, and many others, are available on codeplex.

查看更多
虎瘦雄心在
4楼-- · 2019-01-23 05:45

Take a look at one of these API's:

查看更多
唯我独甜
5楼-- · 2019-01-23 05:52
ZipFile.CreateFromDirectory(<path of folder you need to zip>, <path of zip file with .zip in the end>, CompressionLevel.Fastest, true);
查看更多
登录 后发表回答