How can I Compress a directory with .NET?

前端 未结 10 2710
孤街浪徒
孤街浪徒 2021-02-14 16:16

I have a directory that contains several files. I want compress this folder to a zip or tar.gz file. How can I do his work in C#?

10条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 16:32

    You can use DotNetZip Library. It has quite rich and useful features.


    EDIT:

    string[] MainDirs = Directory.GetDirectories(DirString);
    
    for (int i = 0; i < MainDirs.Length; i++)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.UseUnicodeAsNecessary = true;
            zip.AddDirectory(MainDirs[i]);
            zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
            zip.Save(string.Format("test{0}.zip", i));   
        }
    }
    

提交回复
热议问题