Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

sharpziplib

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

Examples on Google seem to be woefully thin.

like image 922
Josh Kodroff Avatar asked Jan 24 '23 00:01

Josh Kodroff


1 Answers

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();
}
like image 192
Mehmet Ergut Avatar answered Feb 16 '23 03:02

Mehmet Ergut