2025-11-27 15:59:39 +00:00
|
|
|
using System.IO;
|
2022-06-16 23:32:46 +02:00
|
|
|
using System.Text;
|
2020-07-31 10:12:34 -07:00
|
|
|
using SharpCompress.Common;
|
2025-11-27 15:59:39 +00:00
|
|
|
using SharpCompress.Writers;
|
2015-12-30 11:19:42 +00:00
|
|
|
using Xunit;
|
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
namespace SharpCompress.Test.Zip;
|
|
|
|
|
|
|
|
|
|
public class ZipWriterTests : WriterTests
|
2015-12-30 11:19:42 +00:00
|
|
|
{
|
2023-03-21 13:14:08 +00:00
|
|
|
public ZipWriterTests()
|
|
|
|
|
: base(ArchiveType.Zip) { }
|
2020-07-31 09:30:20 -07:00
|
|
|
|
2025-11-27 15:59:39 +00:00
|
|
|
[Fact]
|
|
|
|
|
public void Zip_BZip2_Write_EmptyFile()
|
|
|
|
|
{
|
|
|
|
|
// Test that writing an empty file with BZip2 compression doesn't throw DivideByZeroException
|
|
|
|
|
using var memoryStream = new MemoryStream();
|
|
|
|
|
var options = new WriterOptions(CompressionType.BZip2)
|
|
|
|
|
{
|
|
|
|
|
ArchiveEncoding = new ArchiveEncoding { Default = new UTF8Encoding(false) },
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-15 11:41:30 +00:00
|
|
|
using (var writer = WriterFactory.OpenWriter(memoryStream, ArchiveType.Zip, options))
|
2025-11-27 15:59:39 +00:00
|
|
|
{
|
|
|
|
|
writer.Write("test-folder/zero-byte-file.txt", Stream.Null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.True(memoryStream.Length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Zip_BZip2_Write_EmptyFolder()
|
|
|
|
|
{
|
|
|
|
|
// Test that writing an empty folder entry with BZip2 compression doesn't throw DivideByZeroException
|
|
|
|
|
using var memoryStream = new MemoryStream();
|
|
|
|
|
var options = new WriterOptions(CompressionType.BZip2)
|
|
|
|
|
{
|
|
|
|
|
ArchiveEncoding = new ArchiveEncoding { Default = new UTF8Encoding(false) },
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-15 11:41:30 +00:00
|
|
|
using (var writer = WriterFactory.OpenWriter(memoryStream, ArchiveType.Zip, options))
|
2025-11-27 15:59:39 +00:00
|
|
|
{
|
|
|
|
|
writer.Write("test-empty-folder/", Stream.Null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.True(memoryStream.Length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Zip_Deflate_Write() =>
|
2022-12-20 15:06:44 +00:00
|
|
|
Write(
|
|
|
|
|
CompressionType.Deflate,
|
|
|
|
|
"Zip.deflate.noEmptyDirs.zip",
|
|
|
|
|
"Zip.deflate.noEmptyDirs.zip",
|
|
|
|
|
Encoding.UTF8
|
|
|
|
|
);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Zip_BZip2_Write() =>
|
2022-12-20 15:06:44 +00:00
|
|
|
Write(
|
|
|
|
|
CompressionType.BZip2,
|
|
|
|
|
"Zip.bzip2.noEmptyDirs.zip",
|
|
|
|
|
"Zip.bzip2.noEmptyDirs.zip",
|
|
|
|
|
Encoding.UTF8
|
|
|
|
|
);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Zip_None_Write() =>
|
2022-12-20 15:06:44 +00:00
|
|
|
Write(
|
|
|
|
|
CompressionType.None,
|
|
|
|
|
"Zip.none.noEmptyDirs.zip",
|
|
|
|
|
"Zip.none.noEmptyDirs.zip",
|
|
|
|
|
Encoding.UTF8
|
|
|
|
|
);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Zip_LZMA_Write() =>
|
2022-12-20 15:06:44 +00:00
|
|
|
Write(
|
|
|
|
|
CompressionType.LZMA,
|
|
|
|
|
"Zip.lzma.noEmptyDirs.zip",
|
|
|
|
|
"Zip.lzma.noEmptyDirs.zip",
|
|
|
|
|
Encoding.UTF8
|
|
|
|
|
);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Zip_PPMd_Write() =>
|
2022-12-20 15:06:44 +00:00
|
|
|
Write(
|
|
|
|
|
CompressionType.PPMd,
|
|
|
|
|
"Zip.ppmd.noEmptyDirs.zip",
|
|
|
|
|
"Zip.ppmd.noEmptyDirs.zip",
|
|
|
|
|
Encoding.UTF8
|
|
|
|
|
);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Zip_Rar_Write() =>
|
2025-04-28 16:18:01 +01:00
|
|
|
Assert.Throws<InvalidFormatException>(() =>
|
|
|
|
|
Write(CompressionType.Rar, "Zip.ppmd.noEmptyDirs.zip", "Zip.ppmd.noEmptyDirs.zip")
|
2022-12-20 15:06:44 +00:00
|
|
|
);
|
2015-12-30 11:19:42 +00:00
|
|
|
}
|