Skip to content

CompressionMethod.Stored defective in V 1.2.0 (compared to 1.1.0) #398

Closed

Description

@martinweihrauch

Steps to reproduce

// 1. Set compression level to 0 for the stream
zipStream.SetLevel(0);

// 2. Manually add a zip entry
var newEntry = new ZipEntry(entryName)
{
    DateTime = DateTime.Now,
    Size = mem.Length,
    CompressionMethod = CompressionMethod.Stored,
    AESKeySize = 0,
    IsCrypted = false
};
  1. Then, the resulting ZIP cannot be opened with Windows explorer and in 7Zip, the archive is clearly broken! This is only in Version 1.2.0, not in 1.1.0. I include the complete code below.

Expected behavior

Creating an archive, where all files are stored without compression and the "packed size" is exactly the filesize.

Actual behavior

Broken ZIP archive. Cannot be opened with Windows explorer. 7Zip opens it, but does not show all packed files and under properties shows that there is a header error.

Version of SharpZipLib

1.2.0 only. Version 1.1.0 is correct.

Obtained from (only keep the relevant lines)

  • Package installed using NuGet

Code:

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

namespace TestField
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fsOut = File.Create("F:\\temp\\_test.zip");
            var zipStream = new ZipOutputStream(fsOut);
            zipStream.SetLevel(0); // 0-9, 9 being the highest level of compression
            zipStream.UseZip64 = UseZip64.Dynamic;
            // optional. Null is the same as not setting. Required if using AES.
            zipStream.Password = null; 

            AddUnencrypted("F:\\temp\\jasper1.jpg", zipStream);
            AddUnencrypted("F:\\temp\\jasper2.jpg", zipStream);
            AddUnencrypted("F:\\temp\\jasper3.jpg", zipStream);

            zipStream.IsStreamOwner = false; //  also close the underlying stream
            zipStream.Close();
            fsOut.Close();


        }

        private static void AddUnencrypted(string filename, ZipOutputStream zipStream)
        {
            string entryName = Path.GetFileName(filename);
            // Removes drive from name and fixes slash direction
            entryName = ZipEntry.CleanName(entryName);
            var mem = new MemoryStream(File.ReadAllBytes(filename));
            mem.Position = 0;

            var newEntry = new ZipEntry(entryName)
            {
                DateTime = DateTime.Now,
                Size = mem.Length,
                CompressionMethod = CompressionMethod.Stored,
                AESKeySize = 0,
                IsCrypted = false
            };

            zipStream.PutNextEntry(newEntry);
            byte[] buffer = new byte[4096];
            StreamUtils.Copy(mem, zipStream, buffer);
            Console.WriteLine($"File: {entryName} after Write pos: {zipStream.Position}");
            zipStream.CloseEntry();
            
            var pos = zipStream.Position;
            var len = mem.Length;
            var offset = pos - len;
            Console.WriteLine($"after close position: {pos} offset: {offset} length: {len}");
        }
    }
}

Activity

  1. DavidHollman commented on Dec 4, 2019

    @DavidHollman

    Could this affect FastZip?

  2. Numpsy commented on Dec 4, 2019

    @Numpsy
    Contributor

    FastZip uses ZipOutputStream to create zips, so it could be related.

    I haven't tested it, but it might be that this, #399 and #391 are all variants of the same issue

  3. added
    fixed in next
    zip
    on Dec 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfixed in nextzip

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions