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

C# 如何使用SharpZipLib将文件添加到存档中而不进行压缩?,c#,sharpziplib,C#,Sharpziplib,如何使用SharpZipLib将文件添加到Zip存档中而不进行压缩 谷歌上的例子似乎少得可怜。您可以使用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 =

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


谷歌上的例子似乎少得可怜。

您可以使用
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();
}