3
3
4
4
namespace ICSharpCode . SharpZipLib . GZip
5
5
{
6
+ using static Zip . Compression . Deflater ;
7
+
6
8
/// <summary>
7
9
/// An example class to demonstrate compression and decompression of GZip streams.
8
10
/// </summary>
@@ -15,19 +17,27 @@ public static class GZip
15
17
/// <param name="inStream">The readable stream containing data to decompress.</param>
16
18
/// <param name="outStream">The output stream to receive the decompressed data.</param>
17
19
/// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
20
+ /// <exception cref="ArgumentNullException">Input or output stream is null</exception>
18
21
public static void Decompress ( Stream inStream , Stream outStream , bool isStreamOwner )
19
22
{
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" ) ;
23
28
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 ] ) ;
28
35
}
29
- } finally {
30
- if ( isStreamOwner ) {
36
+ }
37
+ finally
38
+ {
39
+ if ( isStreamOwner )
40
+ {
31
41
// inStream is closed by the GZipInputStream if stream owner
32
42
outStream . Dispose ( ) ;
33
43
}
@@ -41,21 +51,38 @@ public static void Decompress(Stream inStream, Stream outStream, bool isStreamOw
41
51
/// <param name="inStream">The readable stream to compress.</param>
42
52
/// <param name="outStream">The output stream to receive the compressed data.</param>
43
53
/// <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 )
47
60
{
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" ) ;
51
63
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 ] ) ;
56
80
}
57
- } finally {
58
- if ( isStreamOwner ) {
81
+ }
82
+ finally
83
+ {
84
+ if ( isStreamOwner )
85
+ {
59
86
// outStream is closed by the GZipOutputStream if stream owner
60
87
inStream . Dispose ( ) ;
61
88
}
0 commit comments