.Net Zip Up files

后端 未结 6 1324
不思量自难忘°
不思量自难忘° 2020-12-06 02:38

Whats the best way to zip up files using C#? Ideally I want to be able to seperate files into a single archive.

6条回答
  •  我在风中等你
    2020-12-06 03:01

    Take a look at this library: http://www.icsharpcode.net/OpenSource/SharpZipLib/

    It is pretty comprehensive, it deals with many formats, is open-source, and you can use in closed-source commercial applications.

    It is very simple to use:

    byte[] data1 = new byte[...];
    byte[] data2 = new byte[...];
    /*...*/
    
    var path = @"c:\test.zip";
    var zip = new ZipOutputStream(new FileStream(path, FileMode.Create))
                {
                    IsStreamOwner = true
                }
    
    zip.PutNextEntry("File1.txt");
    zip.Write(data1, 0, data1.Length);
    
    zip.PutNextEntry("File2.txt");
    zip.Write(data2, 0, data2.Length);
    
    zip.Close();
    zip.Dispose();
    

提交回复
热议问题