GZip.cs 3.3 KB

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