Chilkat Email Components Home

Compression Method vs. Compression Level

Back

Question:
I've noticed that if i set the compression level to 0, the file sizes have actually increased slightly when i view them in WinZip or WInRAR. Is this extra data some kind of metadata? Also, am i right in assuming that the max file size that can be zipped is 4096MB? If this is the case what happens when i zip a file which is exactly 4096MB with 0 compression level?

Answer:
Yes, the max size is 4096 MB.
Setting the compression level to 0 will cause a slight increase in size. That is because the CompressionMethod is still 8, indicating the Deflate algorithm. Essentially, the data is passing through the deflate algorithm with no compression. You can set the CompressionMethod on each entry to 0, and this causes the exact sizes to be maintained (because the bytes are simply copied to the output).

            Chilkat.Zip zip = new Chilkat.Zip();
            zip.UnlockComponent("anything for 30-day trial");

            zip.NewZip("test.zip");

            zip.AppendOneFileOrDir("fw9.pdf",false);
            //zip.AppendOneFileOrDir("c:/temp/a/setup.exe", false);
            //zip.AppendFiles("c:/temp/a/*", true);
            
            // Set the compression method = 0
            // after all entries are appended:
            int n = zip.NumEntries;
            int i;
            for (i = 0; i < n; i++)
            {
                Chilkat.ZipEntry entry = zip.GetEntryByIndex(i);
                entry.CompressionMethod = 0;
            }
            
            // To write a Zip without compression:
            // Set the compression level to 0 *after* all file references have been
            // added, and just prior to writing the .zip.
            // Setting compression level = 0, but leaving the compression method = 8
            // causes a slight increase in file size (within the zip).  The increase
            // is only a few bytes.  To get an exact pass-through, it is better
            // to set the compression method = 0.
            //zip.SetCompressionLevel(0);

            zip.WriteZipAndClose();