DivideByZeroException throw when using Zip with BZip2 #727

Closed
opened 2026-01-29 22:16:28 +00:00 by claunia · 2 comments
Owner

Originally created by @zhuxb711 on GitHub (Nov 27, 2025).

Originally assigned to: @Copilot on GitHub.

Should fixed this issue since we need to put empty folder or empty files into Zip.
Which is very common scenario. Because you would never know which kinds of data that user wants to compress.

On the other hand, Zip allows empty file or empty folder. We could not filter away those zero-byte files or empty folders which would be very confusing.

WriterOptions Options = new WriterOptions(CompressionType.BZip2)
{
    ArchiveEncoding = new ArchiveEncoding
    {
        Default = new UTF8Encoding(false)
    }
};

using (IWriter Writer = WriterFactory.Open(<OutputStream>, ArchiveType.Zip, Options))
{
     Writer.Write("test-empty-folder/", Stream.Null); // Cause DivideByZeroException
     Writer.Write("test-folder/zero-byte-file.txt", Stream.Null); // Cause DivideByZeroException
}

last is -1 here
9433e06b93/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs (L1417)

Originally created by @zhuxb711 on GitHub (Nov 27, 2025). Originally assigned to: @Copilot on GitHub. Should fixed this issue since we need to put empty folder or empty files into Zip. Which is very common scenario. Because you would never know which kinds of data that user wants to compress. On the other hand, Zip allows empty file or empty folder. We could not filter away those zero-byte files or empty folders which would be very confusing. ```csharp WriterOptions Options = new WriterOptions(CompressionType.BZip2) { ArchiveEncoding = new ArchiveEncoding { Default = new UTF8Encoding(false) } }; using (IWriter Writer = WriterFactory.Open(<OutputStream>, ArchiveType.Zip, Options)) { Writer.Write("test-empty-folder/", Stream.Null); // Cause DivideByZeroException Writer.Write("test-folder/zero-byte-file.txt", Stream.Null); // Cause DivideByZeroException } ``` `last` is -1 here https://github.com/adamhathcock/sharpcompress/blob/9433e06b9313d7a46c1e71219ea5326b95bdbebb/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs#L1417
Author
Owner

@zhuxb711 commented on GitHub (Nov 29, 2025):

Compress using Zip with BZip2 works fine in 0.42 now

@zhuxb711 commented on GitHub (Nov 29, 2025): Compress using Zip with BZip2 works fine in 0.42 now
Author
Owner

@adamhathcock commented on GitHub (Nov 29, 2025):

🥳

@adamhathcock commented on GitHub (Nov 29, 2025): 🥳
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#727