CSharpier formatting.

This commit is contained in:
Nanook
2025-07-24 00:46:09 +01:00
parent c5de3d8cc1
commit e9dd413de6
7 changed files with 174 additions and 73 deletions

View File

@@ -14,7 +14,7 @@ public class WriterOptions : OptionsBase
CompressionType.Deflate => (int)D.CompressionLevel.Default,
CompressionType.Deflate64 => (int)D.CompressionLevel.Default,
CompressionType.GZip => (int)D.CompressionLevel.Default,
_ => 0
_ => 0,
};
}

View File

@@ -364,7 +364,11 @@ public class ZipWriter : AbstractWriter
}
case ZipCompressionMethod.Deflate:
{
return new DeflateStream(counting, CompressionMode.Compress, (CompressionLevel)compressionLevel);
return new DeflateStream(
counting,
CompressionMode.Compress,
(CompressionLevel)compressionLevel
);
}
case ZipCompressionMethod.BZip2:
{
@@ -392,7 +396,10 @@ public class ZipWriter : AbstractWriter
}
case ZipCompressionMethod.ZStandard:
{
return new ZstdSharp.CompressionStream(counting, (int)writer.WriterOptions.CompressionLevel);
return new ZstdSharp.CompressionStream(
counting,
(int)writer.WriterOptions.CompressionLevel
);
}
default:
{

View File

@@ -26,10 +26,15 @@ public class ZipWriterEntryOptions
/// <remarks>
/// This property is deprecated. Use <see cref="CompressionLevel"/> instead.
/// </remarks>
[Obsolete("Use CompressionLevel property instead. This property will be removed in a future version.")]
public CompressionLevel? DeflateCompressionLevel
{
get => CompressionLevel.HasValue ? (CompressionLevel)Math.Min(CompressionLevel.Value, 9) : null;
[Obsolete(
"Use CompressionLevel property instead. This property will be removed in a future version."
)]
public CompressionLevel? DeflateCompressionLevel
{
get =>
CompressionLevel.HasValue
? (CompressionLevel)Math.Min(CompressionLevel.Value, 9)
: null;
set => CompressionLevel = value.HasValue ? (int)value.Value : null;
}

View File

@@ -7,7 +7,10 @@ namespace SharpCompress.Writers.Zip;
public class ZipWriterOptions : WriterOptions
{
public ZipWriterOptions(CompressionType compressionType, CompressionLevel compressionLevel = D.CompressionLevel.Default)
public ZipWriterOptions(
CompressionType compressionType,
CompressionLevel compressionLevel = D.CompressionLevel.Default
)
: base(compressionType, (int)compressionLevel) { }
internal ZipWriterOptions(WriterOptions options)
@@ -43,7 +46,10 @@ public class ZipWriterOptions : WriterOptions
public void SetZStandardCompressionLevel(int level)
{
if (level < 1 || level > 22)
throw new ArgumentOutOfRangeException(nameof(level), "ZStandard compression level must be between 1 and 22");
throw new ArgumentOutOfRangeException(
nameof(level),
"ZStandard compression level must be between 1 and 22"
);
CompressionLevel = level;
}
@@ -55,7 +61,9 @@ public class ZipWriterOptions : WriterOptions
/// <remarks>
/// This property is deprecated. Use <see cref="WriterOptions.CompressionLevel"/> or <see cref="SetDeflateCompressionLevel"/> instead.
/// </remarks>
[Obsolete("Use CompressionLevel property or SetDeflateCompressionLevel method instead. This property will be removed in a future version.")]
[Obsolete(
"Use CompressionLevel property or SetDeflateCompressionLevel method instead. This property will be removed in a future version."
)]
public CompressionLevel DeflateCompressionLevel
{
get => (CompressionLevel)Math.Min(CompressionLevel, 9);

View File

@@ -352,7 +352,11 @@ public class ArchiveTests : ReaderTests
/// <summary>
/// Creates a writer with the specified compression type and level
/// </summary>
protected static IWriter CreateWriterWithLevel(Stream stream, CompressionType compressionType, int? compressionLevel = null)
protected static IWriter CreateWriterWithLevel(
Stream stream,
CompressionType compressionType,
int? compressionLevel = null
)
{
var writerOptions = new ZipWriterOptions(compressionType);
if (compressionLevel.HasValue)
@@ -365,7 +369,10 @@ public class ArchiveTests : ReaderTests
/// <summary>
/// Verifies archive content against expected files with CRC32 validation
/// </summary>
protected void VerifyArchiveContent(MemoryStream zipStream, Dictionary<string, (byte[] data, uint crc)> expectedFiles)
protected void VerifyArchiveContent(
MemoryStream zipStream,
Dictionary<string, (byte[] data, uint crc)> expectedFiles
)
{
zipStream.Position = 0;
using var archive = ArchiveFactory.Open(zipStream);
@@ -378,14 +385,17 @@ public class ArchiveTests : ReaderTests
entryStream.CopyTo(extractedStream);
var extractedData = extractedStream.ToArray();
Assert.True(expectedFiles.ContainsKey(entry.Key.NotNull()), $"Unexpected entry: {entry.Key}");
Assert.True(
expectedFiles.ContainsKey(entry.Key.NotNull()),
$"Unexpected entry: {entry.Key}"
);
var (expectedData, expectedCrc) = expectedFiles[entry.Key.NotNull()];
var actualCrc = CalculateCrc32(extractedData);
Assert.Equal(expectedCrc, actualCrc);
Assert.Equal(expectedData.Length, extractedData.Length);
// For large files, spot check rather than full comparison for performance
if (expectedData.Length > 1024 * 1024)
{
@@ -407,23 +417,37 @@ public class ArchiveTests : ReaderTests
Assert.Equal(expected.Take(1024), actual.Take(1024));
var mid = expected.Length / 2;
Assert.Equal(expected.Skip(mid).Take(1024), actual.Skip(mid).Take(1024));
Assert.Equal(expected.Skip(Math.Max(0, expected.Length - 1024)), actual.Skip(Math.Max(0, actual.Length - 1024)));
Assert.Equal(
expected.Skip(Math.Max(0, expected.Length - 1024)),
actual.Skip(Math.Max(0, actual.Length - 1024))
);
}
/// <summary>
/// Verifies compression ratio meets expectations
/// </summary>
protected void VerifyCompressionRatio(long originalSize, long compressedSize, double maxRatio, string context)
protected void VerifyCompressionRatio(
long originalSize,
long compressedSize,
double maxRatio,
string context
)
{
var compressionRatio = (double)compressedSize / originalSize;
Assert.True(compressionRatio < maxRatio,
$"Expected better compression for {context}. Original: {originalSize}, Compressed: {compressedSize}, Ratio: {compressionRatio:P}");
Assert.True(
compressionRatio < maxRatio,
$"Expected better compression for {context}. Original: {originalSize}, Compressed: {compressedSize}, Ratio: {compressionRatio:P}"
);
}
/// <summary>
/// Creates a memory-based archive with specified files and compression
/// </summary>
protected MemoryStream CreateMemoryArchive(Dictionary<string, byte[]> files, CompressionType compressionType, int? compressionLevel = null)
protected MemoryStream CreateMemoryArchive(
Dictionary<string, byte[]> files,
CompressionType compressionType,
int? compressionLevel = null
)
{
var zipStream = new MemoryStream();
using (var writer = CreateWriterWithLevel(zipStream, compressionType, compressionLevel))
@@ -465,7 +489,8 @@ public class ArchiveTests : ReaderTests
Dictionary<string, byte[]> testFiles,
CompressionType compressionType,
int? compressionLevel = null,
double maxCompressionRatio = 0.8)
double maxCompressionRatio = 0.8
)
{
// Calculate expected CRCs
var expectedFiles = testFiles.ToDictionary(
@@ -480,7 +505,12 @@ public class ArchiveTests : ReaderTests
if (compressionType != CompressionType.None)
{
var originalSize = testFiles.Values.Sum(data => (long)data.Length);
VerifyCompressionRatio(originalSize, zipStream.Length, maxCompressionRatio, compressionType.ToString());
VerifyCompressionRatio(
originalSize,
zipStream.Length,
maxCompressionRatio,
compressionType.ToString()
);
}
// Verify content
@@ -490,11 +520,14 @@ public class ArchiveTests : ReaderTests
/// <summary>
/// Verifies archive entries have correct compression type
/// </summary>
protected void VerifyCompressionType(MemoryStream zipStream, CompressionType expectedCompressionType)
protected void VerifyCompressionType(
MemoryStream zipStream,
CompressionType expectedCompressionType
)
{
zipStream.Position = 0;
using var archive = ArchiveFactory.Open(zipStream);
foreach (var entry in archive.Entries.Where(e => !e.IsDirectory))
{
Assert.Equal(expectedCompressionType, entry.CompressionType);
@@ -504,21 +537,24 @@ public class ArchiveTests : ReaderTests
/// <summary>
/// Extracts and verifies a single entry from archive
/// </summary>
protected (byte[] data, uint crc) ExtractAndVerifyEntry(MemoryStream zipStream, string entryName)
protected (byte[] data, uint crc) ExtractAndVerifyEntry(
MemoryStream zipStream,
string entryName
)
{
zipStream.Position = 0;
using var archive = ArchiveFactory.Open(zipStream);
var entry = archive.Entries.FirstOrDefault(e => e.Key == entryName && !e.IsDirectory);
Assert.NotNull(entry);
using var entryStream = entry.OpenEntryStream();
using var extractedStream = new MemoryStream();
entryStream.CopyTo(extractedStream);
var extractedData = extractedStream.ToArray();
var crc = CalculateCrc32(extractedData);
return (extractedData, crc);
}
}

View File

@@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
namespace SharpCompress.Test.Zip;
/// <summary>
/// Generates pseudo English-style text for testing - Nanook
/// </summary>
@@ -92,6 +93,9 @@ internal class TestPseudoTextStream : Stream
}
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
}

View File

@@ -19,23 +19,28 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests
public ZipTypesLevelsWithCrcRatioTests() => UseExtensionInsteadOfNameToVerify = true;
[Theory]
[InlineData(CompressionType.Deflate, 1, 1, 0.11f)] // was 0.8f, actual 0.104
[InlineData(CompressionType.Deflate, 3, 1, 0.08f)] // was 0.8f, actual 0.078
[InlineData(CompressionType.Deflate, 6, 1, 0.05f)] // was 0.8f, actual ~0.042
[InlineData(CompressionType.Deflate, 9, 1, 0.04f)] // was 0.7f, actual 0.038
[InlineData(CompressionType.ZStandard, 1, 1, 0.025f)] // was 0.8f, actual 0.023
[InlineData(CompressionType.ZStandard, 3, 1, 0.015f)] // was 0.7f, actual 0.013
[InlineData(CompressionType.ZStandard, 9, 1, 0.006f)] // was 0.7f, actual 0.005
[InlineData(CompressionType.Deflate, 1, 1, 0.11f)] // was 0.8f, actual 0.104
[InlineData(CompressionType.Deflate, 3, 1, 0.08f)] // was 0.8f, actual 0.078
[InlineData(CompressionType.Deflate, 6, 1, 0.05f)] // was 0.8f, actual ~0.042
[InlineData(CompressionType.Deflate, 9, 1, 0.04f)] // was 0.7f, actual 0.038
[InlineData(CompressionType.ZStandard, 1, 1, 0.025f)] // was 0.8f, actual 0.023
[InlineData(CompressionType.ZStandard, 3, 1, 0.015f)] // was 0.7f, actual 0.013
[InlineData(CompressionType.ZStandard, 9, 1, 0.006f)] // was 0.7f, actual 0.005
[InlineData(CompressionType.ZStandard, 22, 1, 0.005f)] // was 0.7f, actual 0.004
[InlineData(CompressionType.BZip2, 0, 1, 0.035f)] // was 0.8f, actual 0.033
[InlineData(CompressionType.LZMA, 0, 1, 0.005f)] // was 0.8f, actual 0.004
[InlineData(CompressionType.None, 0, 1, 1.001f)] // was 1.1f, actual 1.000
[InlineData(CompressionType.Deflate, 6, 2, 0.045f)] // was 0.8f, actual 0.042
[InlineData(CompressionType.ZStandard, 3, 2, 0.012f)] // was 0.7f, actual 0.010
[InlineData(CompressionType.BZip2, 0, 2, 0.035f)] // was 0.8f, actual 0.032
[InlineData(CompressionType.Deflate, 9, 3, 0.04f)] // was 0.7f, actual 0.038
[InlineData(CompressionType.ZStandard, 9, 3, 0.003f)] // was 0.7f, actual 0.002
public void Zip_Create_Archive_With_3_Files_Crc32_Test(CompressionType compressionType, int compressionLevel, int sizeMb, float expectedRatio)
[InlineData(CompressionType.BZip2, 0, 1, 0.035f)] // was 0.8f, actual 0.033
[InlineData(CompressionType.LZMA, 0, 1, 0.005f)] // was 0.8f, actual 0.004
[InlineData(CompressionType.None, 0, 1, 1.001f)] // was 1.1f, actual 1.000
[InlineData(CompressionType.Deflate, 6, 2, 0.045f)] // was 0.8f, actual 0.042
[InlineData(CompressionType.ZStandard, 3, 2, 0.012f)] // was 0.7f, actual 0.010
[InlineData(CompressionType.BZip2, 0, 2, 0.035f)] // was 0.8f, actual 0.032
[InlineData(CompressionType.Deflate, 9, 3, 0.04f)] // was 0.7f, actual 0.038
[InlineData(CompressionType.ZStandard, 9, 3, 0.003f)] // was 0.7f, actual 0.002
public void Zip_Create_Archive_With_3_Files_Crc32_Test(
CompressionType compressionType,
int compressionLevel,
int sizeMb,
float expectedRatio
)
{
const int OneMiB = 1024 * 1024;
var baseSize = sizeMb * OneMiB;
@@ -49,7 +54,7 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests
{
[$"file1_{sizeMb}MiB.txt"] = (file1Data, CalculateCrc32(file1Data)),
[$"data/file2_{sizeMb * 2}MiB.txt"] = (file2Data, CalculateCrc32(file2Data)),
[$"deep/nested/file3_{sizeMb * 3}MiB.txt"] = (file3Data, CalculateCrc32(file3Data))
[$"deep/nested/file3_{sizeMb * 3}MiB.txt"] = (file3Data, CalculateCrc32(file3Data)),
};
// Create zip archive in memory
@@ -69,11 +74,19 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests
// Verify compression occurred (except for None compression type)
if (compressionType != CompressionType.None)
{
Assert.True(zipStream.Length < originalSize, $"Compression failed: compressed={zipStream.Length}, original={originalSize}");
Assert.True(
zipStream.Length < originalSize,
$"Compression failed: compressed={zipStream.Length}, original={originalSize}"
);
}
// Verify compression ratio
VerifyCompressionRatio(originalSize, zipStream.Length, expectedRatio, $"{compressionType} level {compressionLevel}");
VerifyCompressionRatio(
originalSize,
zipStream.Length,
expectedRatio,
$"{compressionType} level {compressionLevel}"
);
// Verify archive content and CRC32
VerifyArchiveContent(zipStream, expectedFiles);
@@ -83,17 +96,22 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests
}
[Theory]
[InlineData(CompressionType.Deflate, 1, 4, 0.11f)] // was 0.8, actual 0.105
[InlineData(CompressionType.Deflate, 3, 4, 0.08f)] // was 0.8, actual 0.077
[InlineData(CompressionType.Deflate, 6, 4, 0.045f)] // was 0.8, actual 0.042
[InlineData(CompressionType.Deflate, 9, 4, 0.04f)] // was 0.8, actual 0.037
[InlineData(CompressionType.ZStandard, 1, 4, 0.025f)] // was 0.8, actual 0.022
[InlineData(CompressionType.ZStandard, 3, 4, 0.012f)] // was 0.8, actual 0.010
[InlineData(CompressionType.ZStandard, 9, 4, 0.003f)] // was 0.8, actual 0.002
[InlineData(CompressionType.Deflate, 1, 4, 0.11f)] // was 0.8, actual 0.105
[InlineData(CompressionType.Deflate, 3, 4, 0.08f)] // was 0.8, actual 0.077
[InlineData(CompressionType.Deflate, 6, 4, 0.045f)] // was 0.8, actual 0.042
[InlineData(CompressionType.Deflate, 9, 4, 0.04f)] // was 0.8, actual 0.037
[InlineData(CompressionType.ZStandard, 1, 4, 0.025f)] // was 0.8, actual 0.022
[InlineData(CompressionType.ZStandard, 3, 4, 0.012f)] // was 0.8, actual 0.010
[InlineData(CompressionType.ZStandard, 9, 4, 0.003f)] // was 0.8, actual 0.002
[InlineData(CompressionType.ZStandard, 22, 4, 0.003f)] // was 0.8, actual 0.002
[InlineData(CompressionType.BZip2, 0, 4, 0.035f)] // was 0.8, actual 0.032
[InlineData(CompressionType.LZMA, 0, 4, 0.003f)] // was 0.8, actual 0.002
public void Zip_WriterFactory_Crc32_Test(CompressionType compressionType, int compressionLevel, int sizeMb, float expectedRatio)
[InlineData(CompressionType.BZip2, 0, 4, 0.035f)] // was 0.8, actual 0.032
[InlineData(CompressionType.LZMA, 0, 4, 0.003f)] // was 0.8, actual 0.002
public void Zip_WriterFactory_Crc32_Test(
CompressionType compressionType,
int compressionLevel,
int sizeMb,
float expectedRatio
)
{
var fileSize = sizeMb * 1024 * 1024;
@@ -102,18 +120,29 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests
// Create archive with specified compression level
using var zipStream = new MemoryStream();
var writerOptions = new ZipWriterOptions(compressionType) { CompressionLevel = compressionLevel };
var writerOptions = new ZipWriterOptions(compressionType)
{
CompressionLevel = compressionLevel,
};
using (var writer = WriterFactory.Open(zipStream, ArchiveType.Zip, writerOptions))
{
writer.Write($"{compressionType}_level_{compressionLevel}_{sizeMb}MiB.txt", new MemoryStream(testData));
writer.Write(
$"{compressionType}_level_{compressionLevel}_{sizeMb}MiB.txt",
new MemoryStream(testData)
);
}
// Calculate and output actual compression ratio
var actualRatio = (double)zipStream.Length / testData.Length;
//Debug.WriteLine($"Zip_WriterFactory_Crc32_Test: {compressionType} Level={compressionLevel} Size={sizeMb}MB Expected={expectedRatio:F3} Actual={actualRatio:F3}");
VerifyCompressionRatio(testData.Length, zipStream.Length, expectedRatio, $"{compressionType} level {compressionLevel}");
VerifyCompressionRatio(
testData.Length,
zipStream.Length,
expectedRatio,
$"{compressionType} level {compressionLevel}"
);
// Verify the archive
zipStream.Position = 0;
@@ -134,17 +163,22 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests
}
[Theory]
[InlineData(CompressionType.Deflate, 1, 2, 0.11f)] // was 0.8, actual 0.104
[InlineData(CompressionType.Deflate, 3, 2, 0.08f)] // was 0.8, actual 0.077
[InlineData(CompressionType.Deflate, 6, 2, 0.045f)] // was 0.8, actual 0.042
[InlineData(CompressionType.Deflate, 9, 2, 0.04f)] // was 0.7, actual 0.038
[InlineData(CompressionType.ZStandard, 1, 2, 0.025f)] // was 0.8, actual 0.023
[InlineData(CompressionType.ZStandard, 3, 2, 0.015f)] // was 0.7, actual 0.012
[InlineData(CompressionType.ZStandard, 9, 2, 0.006f)] // was 0.7, actual 0.005
[InlineData(CompressionType.Deflate, 1, 2, 0.11f)] // was 0.8, actual 0.104
[InlineData(CompressionType.Deflate, 3, 2, 0.08f)] // was 0.8, actual 0.077
[InlineData(CompressionType.Deflate, 6, 2, 0.045f)] // was 0.8, actual 0.042
[InlineData(CompressionType.Deflate, 9, 2, 0.04f)] // was 0.7, actual 0.038
[InlineData(CompressionType.ZStandard, 1, 2, 0.025f)] // was 0.8, actual 0.023
[InlineData(CompressionType.ZStandard, 3, 2, 0.015f)] // was 0.7, actual 0.012
[InlineData(CompressionType.ZStandard, 9, 2, 0.006f)] // was 0.7, actual 0.005
[InlineData(CompressionType.ZStandard, 22, 2, 0.005f)] // was 0.7, actual 0.004
[InlineData(CompressionType.BZip2, 0, 2, 0.035f)] // was 0.8, actual 0.032
[InlineData(CompressionType.LZMA, 0, 2, 0.005f)] // was 0.8, actual 0.004
public void Zip_ZipArchiveOpen_Crc32_Test(CompressionType compressionType, int compressionLevel, int sizeMb, float expectedRatio)
[InlineData(CompressionType.BZip2, 0, 2, 0.035f)] // was 0.8, actual 0.032
[InlineData(CompressionType.LZMA, 0, 2, 0.005f)] // was 0.8, actual 0.004
public void Zip_ZipArchiveOpen_Crc32_Test(
CompressionType compressionType,
int compressionLevel,
int sizeMb,
float expectedRatio
)
{
var fileSize = sizeMb * 1024 * 1024;
@@ -155,7 +189,10 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests
using var zipStream = new MemoryStream();
using (var writer = CreateWriterWithLevel(zipStream, compressionType, compressionLevel))
{
writer.Write($"{compressionType}_{compressionLevel}_{sizeMb}MiB.txt", new MemoryStream(testData));
writer.Write(
$"{compressionType}_{compressionLevel}_{sizeMb}MiB.txt",
new MemoryStream(testData)
);
}
// Calculate and output actual compression ratio
@@ -188,7 +225,11 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests
VerifyDataSpotCheck(testData, extractedData);
}
VerifyCompressionRatio(testData.Length, zipStream.Length, expectedRatio, $"{compressionType} Level {compressionLevel}");
VerifyCompressionRatio(
testData.Length,
zipStream.Length,
expectedRatio,
$"{compressionType} Level {compressionLevel}"
);
}
}