Skip to content

Fix DivideByZeroException when compressing empty files with BZip2#1043

Merged
adamhathcock merged 2 commits intomasterfrom
copilot/fix-divide-by-zero-exception
Nov 27, 2025
Merged

Fix DivideByZeroException when compressing empty files with BZip2#1043
adamhathcock merged 2 commits intomasterfrom
copilot/fix-divide-by-zero-exception

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 27, 2025

Writing empty files or folders to a Zip archive with BZip2 compression throws DivideByZeroException in MainSort() due to (i % (last + 1)) evaluating to (i % 0) when last = -1.

var options = new WriterOptions(CompressionType.BZip2);
using var writer = WriterFactory.Open(stream, ArchiveType.Zip, options);
writer.Write("empty-folder/", Stream.Null);  // throws DivideByZeroException

Changes

  • CBZip2OutputStream.cs: Early return in EndBlock() when last < 0 to skip block processing for empty input
  • ZipWriterTests.cs: Added tests for empty file and empty folder scenarios with BZip2 compression
Original prompt

This section details on the original issue you should resolve

<issue_title>DivideByZeroException throw when using Zip with BZip2</issue_title>
<issue_description>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

block[last + i + 2] = block[(i % (last + 1)) + 1];
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix DivideByZeroException when using Zip with BZip2 Fix DivideByZeroException when compressing empty files with BZip2 Nov 27, 2025
Copilot AI requested a review from adamhathcock November 27, 2025 16:04
@adamhathcock adamhathcock marked this pull request as ready for review November 27, 2025 16:05
Copilot AI review requested due to automatic review settings November 27, 2025 16:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a DivideByZeroException that occurs when writing empty files or folders to a Zip archive using BZip2 compression. The exception was caused by the modulo operation (i % (last + 1)) in MainSort() evaluating to (i % 0) when last = -1 (indicating no data was written to the block).

  • Added an early return in CBZip2OutputStream.EndBlock() to skip block processing when no data has been written (last < 0)
  • Added test coverage for both empty file and empty folder scenarios with BZip2 compression

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs Adds early return in EndBlock() to prevent divide-by-zero when compressing empty input
tests/SharpCompress.Test/Zip/ZipWriterTests.cs Adds regression tests for empty file and folder scenarios with BZip2 compression

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/SharpCompress.Test/Zip/ZipWriterTests.cs
Comment thread tests/SharpCompress.Test/Zip/ZipWriterTests.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DivideByZeroException throw when using Zip with BZip2

3 participants