如何使用SharpZipLib将文件添加到存档中而不进行压缩?

4

如何使用SharpZipLib向Zip归档添加无压缩的文件?

谷歌上的示例似乎非常稀少。

1个回答

9

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

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);
}

编辑:实际上,在查看文档后,有一个更简单的方法。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接