[PR #1043] [MERGED] Fix DivideByZeroException when compressing empty files with BZip2 #1466

Closed
opened 2026-01-29 22:20:42 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/1043
Author: @Copilot
Created: 11/27/2025
Status: Merged
Merged: 11/27/2025
Merged by: @adamhathcock

Base: masterHead: copilot/fix-divide-by-zero-exception


📝 Commits (2)

  • cc4d281 Initial plan
  • 6d73c5b Fix DivideByZeroException when using BZip2 with empty files

📊 Changes

2 files changed (+44 additions, -0 deletions)

View changed files

📝 src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs (+6 -0)
📝 tests/SharpCompress.Test/Zip/ZipWriterTests.cs (+38 -0)

📄 Description

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
9433e06b93/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs (L1417)</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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/adamhathcock/sharpcompress/pull/1043 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 11/27/2025 **Status:** ✅ Merged **Merged:** 11/27/2025 **Merged by:** [@adamhathcock](https://github.com/adamhathcock) **Base:** `master` ← **Head:** `copilot/fix-divide-by-zero-exception` --- ### 📝 Commits (2) - [`cc4d281`](https://github.com/adamhathcock/sharpcompress/commit/cc4d28193c49eda80a467b2799c939235a307efc) Initial plan - [`6d73c5b`](https://github.com/adamhathcock/sharpcompress/commit/6d73c5b295f2675daa0c32777ef8606e7d1eea83) Fix DivideByZeroException when using BZip2 with empty files ### 📊 Changes **2 files changed** (+44 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs` (+6 -0) 📝 `tests/SharpCompress.Test/Zip/ZipWriterTests.cs` (+38 -0) </details> ### 📄 Description 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`. ```csharp 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 <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *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. > > ```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</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes adamhathcock/sharpcompress#1039 <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/adamhathcock/sharpcompress/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 22:20:42 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1466