Replace IsLzma2 flag with CompressionType enum for 7z writer API consistency

This commit is contained in:
Daniil Bystrukhin
2026-02-22 21:08:34 -06:00
parent cac77aaeda
commit bcf628568c
4 changed files with 15 additions and 23 deletions

View File

@@ -9,6 +9,7 @@ public enum CompressionType
Deflate,
Rar,
LZMA,
LZMA2,
BCJ,
BCJ2,
LZip,

View File

@@ -74,14 +74,6 @@ public partial class SevenZipWriter : AbstractWriter
}
// Compress file data to output stream
// TODO: LZMA2 encoding is not yet implemented in SharpCompress's LzmaStream
if (sevenZipOptions.IsLzma2)
{
throw new ArchiveOperationException(
"LZMA2 encoding is not yet implemented. Use LZMA (IsLzma2 = false) instead."
);
}
var compressor = new SevenZipStreamsCompressor(OutputStream.NotNull());
var packed = compressor.Compress(
progressStream,

View File

@@ -15,12 +15,22 @@ public sealed record SevenZipWriterOptions : IWriterOptions
private int _compressionLevel;
/// <summary>
/// The compression type to use. Supported: LZMA (default), LZMA2 (via CompressionType.LZMA with IsLzma2=true).
/// The compression type to use. Only LZMA is supported in this version.
/// </summary>
public CompressionType CompressionType
{
get => _compressionType;
init => _compressionType = value;
init
{
if (value != CompressionType.LZMA)
{
throw new ArgumentException(
$"SevenZipWriter only supports CompressionType.LZMA. Got: {value}",
nameof(value)
);
}
_compressionType = value;
}
}
/// <summary>
@@ -54,11 +64,6 @@ public sealed record SevenZipWriterOptions : IWriterOptions
public CompressionProviderRegistry Providers { get; init; } =
CompressionProviderRegistry.Default;
/// <summary>
/// Whether to use LZMA2 instead of LZMA. Default is false (LZMA).
/// </summary>
public bool IsLzma2 { get; init; }
/// <summary>
/// Whether to compress the archive header itself using LZMA.
/// Default is true, matching standard 7-Zip behavior.

View File

@@ -162,15 +162,9 @@ public class SevenZipWriterTests : TestBase
[Fact]
public void SevenZipWriter_LZMA2_ThrowsNotSupported()
{
// LZMA2 encoding is not yet implemented in SharpCompress's LzmaStream
using var archiveStream = new MemoryStream();
using var writer = new SevenZipWriter(
archiveStream,
new SevenZipWriterOptions { IsLzma2 = true }
Assert.Throws<ArgumentException>(
() => new SevenZipWriterOptions(CompressionType.LZMA2)
);
using var source = new MemoryStream("test"u8.ToArray());
Assert.Throws<ArchiveOperationException>(() => writer.Write("test.txt", source, DateTime.UtcNow));
}
[Fact]