4

How do I add files to a Zip archive using SharpZipLib with no compression?

Examples on Google seem to be woefully thin.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148

1 Answers1

9

You can set compression level to 0 using the SetLevel method of the ZipOutputStream class.

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

EDIT: actually, after reviewing documentation, there is a much simpler method.

using (ZipFile z = ZipFile.Create("test.zip"))
{
    z.BeginUpdate();
    z.Add("test.txt", CompressionMethod.Stored);
    z.CommitUpdate();
}
Mehmet Ergut
  • 1,034
  • 8
  • 9