SharpZipLib icon indicating copy to clipboard operation
SharpZipLib copied to clipboard

Use CompressionMethod.Stored and flush the ZipOutputStream after each entity was added leads Zip corrupted in SharpZipLib 1.4.2

Open zhuxb711 opened this issue 1 year ago • 9 comments

Describe the bug

I found a bug that if I set CompressionMethod = CompressionMethod.Stored in the entry and flush the ZipOutputStream, the output zip is corrupted and only the first file exists in the zip file.

using (Stream NewZipStream = File.OpenWrite(Path.GetRandomFileName()))
using (ZipOutputStream ZipStream = new ZipOutputStream(NewZipStream , StringCodec.FromEncoding(Encoding.UTF8)))
{
    ZipStream.SetLevel(0);
    ZipStream.UseZip64 = UseZip64.Dynamic;
    ZipStream.IsStreamOwner = false;

    foreach (string FilePath in Directory.EnumerateFiles("<Your folder that has multiple files>"))
    {
          using (Stream FileStream = File.OpenRead(FilePath))
          {
              ZipEntry NewEntry = new ZipEntry(File.Name)
              {
                  DateTime = DateTime.Now,
                  CompressionMethod = CompressionMethod.Stored,
                  Size = FileStream.Length
              };
          
              await ZipStream.PutNextEntryAsync(NewEntry, CancelToken);
              await FileStream.CopyToAsync(ZipStream);
              await ZipStream.CloseEntryAsync(CancelToken);
          }

          await ZipStream.FlushAsync(CancelToken);
    }
}

Reproduction Code

No response

Steps to reproduce

  1. Use ZipOutputStream
  2. Set CompressionMethod to CompressionMethod.Stored on ZipEntry
  3. Add multiple files/folders into ZipOutputStream
  4. Flush the ZipOutputStream once each entity was added
  5. Found the zip file is corrupted

Expected behavior

Zip is not corrupted

Operating System

Windows

Framework Version

.NET 7

Tags

ZIP

Additional context

No response

zhuxb711 avatar May 23 '23 16:05 zhuxb711