Search code examples
c#sharpziplib

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


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

Examples on Google seem to be woefully thin.


Solution

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