GZip.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.IO;
  3. namespace ICSharpCode.SharpZipLib.GZip
  4. {
  5. using static Zip.Compression.Deflater;
  6. /// <summary>
  7. /// An example class to demonstrate compression and decompression of GZip streams.
  8. /// </summary>
  9. public static class GZip
  10. {
  11. /// <summary>
  12. /// Decompress the <paramref name="inStream">input</paramref> writing
  13. /// uncompressed data to the <paramref name="outStream">output stream</paramref>
  14. /// </summary>
  15. /// <param name="inStream">The readable stream containing data to decompress.</param>
  16. /// <param name="outStream">The output stream to receive the decompressed data.</param>
  17. /// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
  18. /// <exception cref="ArgumentNullException">Input or output stream is null</exception>
  19. public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner)
  20. {
  21. if (inStream == null)
  22. throw new ArgumentNullException(nameof(inStream), "Input stream is null");
  23. if (outStream == null)
  24. throw new ArgumentNullException(nameof(outStream), "Output stream is null");
  25. try
  26. {
  27. using (GZipInputStream gzipInput = new GZipInputStream(inStream))
  28. {
  29. gzipInput.IsStreamOwner = isStreamOwner;
  30. Core.StreamUtils.Copy(gzipInput, outStream, new byte[4096]);
  31. }
  32. }
  33. finally
  34. {
  35. if (isStreamOwner)
  36. {
  37. // inStream is closed by the GZipInputStream if stream owner
  38. outStream.Dispose();
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// Compress the <paramref name="inStream">input stream</paramref> sending
  44. /// result data to <paramref name="outStream">output stream</paramref>
  45. /// </summary>
  46. /// <param name="inStream">The readable stream to compress.</param>
  47. /// <param name="outStream">The output stream to receive the compressed data.</param>
  48. /// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
  49. /// <param name="bufferSize">Deflate buffer size, minimum 512</param>
  50. /// <param name="level">Deflate compression level, 0-9</param>
  51. /// <exception cref="ArgumentNullException">Input or output stream is null</exception>
  52. /// <exception cref="ArgumentOutOfRangeException">Buffer Size is smaller than 512</exception>
  53. /// <exception cref="ArgumentOutOfRangeException">Compression level outside 0-9</exception>
  54. public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int bufferSize = 512, int level = 6)
  55. {
  56. if (inStream == null)
  57. throw new ArgumentNullException(nameof(inStream), "Input stream is null");
  58. if (outStream == null)
  59. throw new ArgumentNullException(nameof(outStream), "Output stream is null");
  60. if (bufferSize < 512)
  61. throw new ArgumentOutOfRangeException(nameof(bufferSize), "Deflate buffer size must be >= 512");
  62. if (level < NO_COMPRESSION || level > BEST_COMPRESSION)
  63. throw new ArgumentOutOfRangeException(nameof(level), "Compression level must be 0-9");
  64. try
  65. {
  66. using (GZipOutputStream gzipOutput = new GZipOutputStream(outStream, bufferSize))
  67. {
  68. gzipOutput.SetLevel(level);
  69. gzipOutput.IsStreamOwner = isStreamOwner;
  70. Core.StreamUtils.Copy(inStream, gzipOutput, new byte[bufferSize]);
  71. }
  72. }
  73. finally
  74. {
  75. if (isStreamOwner)
  76. {
  77. // outStream is closed by the GZipOutputStream if stream owner
  78. inStream.Dispose();
  79. }
  80. }
  81. }
  82. }
  83. }