如何使用SharpZipLib在没有压缩的情况下将文件添加到存档?

Jos*_*off 4 c# sharpziplib

如何使用没有压缩的SharpZipLib将文件添加到Zip存档?

谷歌上的例子看起来很糟糕.

Meh*_*gut 7

您可以使用类的SetLevel方法将压缩级别设置为0 ZipOutputStream.

using (ZipOutputStream s = new ZipOutputStream(File.Create("test.zip")))
{
    s.SetLevel(0); // 0 - store only to 9 - means best compression

    string file = "test.txt";

    byte[] contents = File.ReadAllBytes(file);

    ZipEntry entry = new ZipEntry(Path.GetFileName(file));
    s.PutNextEntry(entry);
    s.Write(contents, 0, contents.Length);
}
Run Code Online (Sandbox Code Playgroud)

编辑:实际上,在审阅文档后,有一个更简单的方法.

using (ZipFile z = ZipFile.Create("test.zip"))
{
    z.BeginUpdate();
    z.Add("test.txt", CompressionMethod.Stored);
    z.CommitUpdate();
}
Run Code Online (Sandbox Code Playgroud)