Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
zhuxb711 opened this issue May 23, 2023 · 9 comments
Labels
bug zip Related to ZIP file format

Comments

@zhuxb711
Copy link

zhuxb711 commented May 23, 2023

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 zhuxb711 added the bug label May 23, 2023
@github-actions github-actions bot added the zip Related to ZIP file format label May 23, 2023
@SourceproStudio

This comment was marked as off-topic.

@piksel
Copy link
Member

piksel commented May 23, 2023

only the first file exists in the zip file.

You are adding the same file many times, using the same name. Most archivers would probably only display that as a single file entry.

Could you upload the output file to ArchiveDiag and post the URL to the resulting analysis here?

@zhuxb711
Copy link
Author

@piksel Yes that just a sample, just let you know that I added multiple files

@zhuxb711
Copy link
Author

Sorry, further test shows that this issue related to FlushAsync() on ZipOutputStream, here is the demo.
Make sure there are some files in your "~\Picures\Screenshot" folder

SharpZipLibTest.zip

@zhuxb711
Copy link
Author

In short, if CompressionMethod is not CompressionMethod.Stored or do not flush the ZipOutputStream after insert a new entry, this issue disappeared.

@piksel
Copy link
Member

piksel commented May 23, 2023

I tried to reproduce your issue but it works correctly:
https://dotnetfiddle.net/IDUcxf

@zhuxb711
Copy link
Author

I have tested in two different PCs using the demo I provided before. And the result is the same, please try use the demo application instead:
image

@zhuxb711
Copy link
Author

zhuxb711 commented May 24, 2023

Demo application SharpZipLibTest.zip will try zip your Screenshots folder in 4 different ways, output zip file will be placed on your desktop. Test1.zip is corrupted and Test2.zip & Test3.zip Test4.zip are correct. Please check code of the demo application

@zhuxb711 zhuxb711 changed the title Set CompressionMethod to CompressionMethod.Stored leads Zip corrupted in SharpZipLib 1.4.2 Use CompressionMethod.Stored and flush the ZipOutputStream after each entity was added leads Zip corrupted in SharpZipLib 1.4.2 May 24, 2023
@zhuxb711
Copy link
Author

@piksel Could you reproduce this issue using the demo application?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug zip Related to ZIP file format
Projects
None yet
Development

No branches or pull requests

3 participants