Skip to content

Commit 083ca7a

Browse files
authored
Merge PR #241, Fix Gzip.Compress arguments
* Fix Gzip.Compress and Gzip.Uncompress Fixes #151 Gzip.Compress does not take a "block size" but rather a buffer size * Add support for passing compression level to GZip.Compress Lower the minimum compression level on GzipOutputStream to the same minimum as Deflater.SetLevel
1 parent 4ee3b24 commit 083ca7a

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

src/ICSharpCode.SharpZipLib/GZip/GZip.cs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace ICSharpCode.SharpZipLib.GZip
55
{
6+
using static Zip.Compression.Deflater;
7+
68
/// <summary>
79
/// An example class to demonstrate compression and decompression of GZip streams.
810
/// </summary>
@@ -15,19 +17,27 @@ public static class GZip
1517
/// <param name="inStream">The readable stream containing data to decompress.</param>
1618
/// <param name="outStream">The output stream to receive the decompressed data.</param>
1719
/// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
20+
/// <exception cref="ArgumentNullException">Input or output stream is null</exception>
1821
public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner)
1922
{
20-
if (inStream == null || outStream == null) {
21-
throw new Exception("Null Stream");
22-
}
23+
if (inStream == null)
24+
throw new ArgumentNullException(nameof(inStream), "Input stream is null");
25+
26+
if (outStream == null)
27+
throw new ArgumentNullException(nameof(outStream), "Output stream is null");
2328

24-
try {
25-
using (GZipInputStream bzipInput = new GZipInputStream(inStream)) {
26-
bzipInput.IsStreamOwner = isStreamOwner;
27-
Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]);
29+
try
30+
{
31+
using (GZipInputStream gzipInput = new GZipInputStream(inStream))
32+
{
33+
gzipInput.IsStreamOwner = isStreamOwner;
34+
Core.StreamUtils.Copy(gzipInput, outStream, new byte[4096]);
2835
}
29-
} finally {
30-
if (isStreamOwner) {
36+
}
37+
finally
38+
{
39+
if (isStreamOwner)
40+
{
3141
// inStream is closed by the GZipInputStream if stream owner
3242
outStream.Dispose();
3343
}
@@ -41,21 +51,38 @@ public static void Decompress(Stream inStream, Stream outStream, bool isStreamOw
4151
/// <param name="inStream">The readable stream to compress.</param>
4252
/// <param name="outStream">The output stream to receive the compressed data.</param>
4353
/// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
44-
/// <param name="level">Block size acts as compression level (1 to 9) with 1 giving
45-
/// the lowest compression and 9 the highest.</param>
46-
public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level)
54+
/// <param name="bufferSize">Deflate buffer size, minimum 512</param>
55+
/// <param name="level">Deflate compression level, 0-9</param>
56+
/// <exception cref="ArgumentNullException">Input or output stream is null</exception>
57+
/// <exception cref="ArgumentOutOfRangeException">Buffer Size is smaller than 512</exception>
58+
/// <exception cref="ArgumentOutOfRangeException">Compression level outside 0-9</exception>
59+
public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int bufferSize = 512, int level = 6)
4760
{
48-
if (inStream == null || outStream == null) {
49-
throw new Exception("Null Stream");
50-
}
61+
if (inStream == null)
62+
throw new ArgumentNullException(nameof(inStream), "Input stream is null");
5163

52-
try {
53-
using (GZipOutputStream bzipOutput = new GZipOutputStream(outStream, level)) {
54-
bzipOutput.IsStreamOwner = isStreamOwner;
55-
Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]);
64+
if(outStream == null)
65+
throw new ArgumentNullException(nameof(outStream), "Output stream is null");
66+
67+
if (bufferSize < 512)
68+
throw new ArgumentOutOfRangeException(nameof(bufferSize), "Deflate buffer size must be >= 512");
69+
70+
if (level<NO_COMPRESSION || level> BEST_COMPRESSION)
71+
throw new ArgumentOutOfRangeException(nameof(level), "Compression level must be 0-9");
72+
73+
try
74+
{
75+
using (GZipOutputStream gzipOutput = new GZipOutputStream(outStream, bufferSize))
76+
{
77+
gzipOutput.SetLevel(level);
78+
gzipOutput.IsStreamOwner = isStreamOwner;
79+
Core.StreamUtils.Copy(inStream, gzipOutput, new byte[bufferSize]);
5680
}
57-
} finally {
58-
if (isStreamOwner) {
81+
}
82+
finally
83+
{
84+
if (isStreamOwner)
85+
{
5986
// outStream is closed by the GZipOutputStream if stream owner
6087
inStream.Dispose();
6188
}

src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public GZipOutputStream(Stream baseOutputStream)
8080

8181
#region Public API
8282
/// <summary>
83-
/// Sets the active compression level (1-9). The new level will be activated
83+
/// Sets the active compression level (0-9). The new level will be activated
8484
/// immediately.
8585
/// </summary>
8686
/// <param name="level">The compression level to set.</param>
@@ -90,9 +90,9 @@ public GZipOutputStream(Stream baseOutputStream)
9090
/// <see cref="Deflater"/>
9191
public void SetLevel(int level)
9292
{
93-
if (level < Deflater.BEST_SPEED) {
94-
throw new ArgumentOutOfRangeException(nameof(level));
95-
}
93+
if (level < Deflater.NO_COMPRESSION || level > Deflater.BEST_COMPRESSION)
94+
throw new ArgumentOutOfRangeException(nameof(level), "Compression level must be 0-9");
95+
9696
deflater_.SetLevel(level);
9797
}
9898

0 commit comments

Comments
 (0)