BZip2.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // BZip2.cs
  2. //
  3. // Copyright (C) 2010 David Pierson
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. //
  19. // Linking this library statically or dynamically with other modules is
  20. // making a combined work based on this library. Thus, the terms and
  21. // conditions of the GNU General Public License cover the whole
  22. // combination.
  23. //
  24. // As a special exception, the copyright holders of this library give you
  25. // permission to link this library with independent modules to produce an
  26. // executable, regardless of the license terms of these independent
  27. // modules, and to copy and distribute the resulting executable under
  28. // terms of your choice, provided that you also meet, for each linked
  29. // independent module, the terms and conditions of the license of that
  30. // module. An independent module is a module which is not derived from
  31. // or based on this library. If you modify this library, you may extend
  32. // this exception to your version of the library, but you are not
  33. // obligated to do so. If you do not wish to do so, delete this
  34. // exception statement from your version.
  35. // Suppress this in CF and 1.1, not needed. Static classes introduced in C# version 2.0
  36. #if !NETCF_2_0 && !NET_1_1
  37. using System;
  38. using System.IO;
  39. namespace CommonMPQ.SharpZipLib.BZip2 {
  40. /// <summary>
  41. /// An example class to demonstrate compression and decompression of BZip2 streams.
  42. /// </summary>
  43. public static class BZip2
  44. {
  45. /// <summary>
  46. /// Decompress the <paramref name="inStream">input</paramref> writing
  47. /// uncompressed data to the <paramref name="outStream">output stream</paramref>
  48. /// </summary>
  49. /// <param name="inStream">The readable stream containing data to decompress.</param>
  50. /// <param name="outStream">The output stream to receive the decompressed data.</param>
  51. /// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
  52. public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner)
  53. {
  54. if (inStream == null || outStream == null) {
  55. throw new Exception("Null Stream");
  56. }
  57. try {
  58. using (BZip2InputStream bzipInput = new BZip2InputStream(inStream)) {
  59. bzipInput.IsStreamOwner = isStreamOwner;
  60. Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]);
  61. }
  62. } finally {
  63. if (isStreamOwner) {
  64. // inStream is closed by the BZip2InputStream if stream owner
  65. outStream.Close();
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Compress the <paramref name="inStream">input stream</paramref> sending
  71. /// result data to <paramref name="outStream">output stream</paramref>
  72. /// </summary>
  73. /// <param name="inStream">The readable stream to compress.</param>
  74. /// <param name="outStream">The output stream to receive the compressed data.</param>
  75. /// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
  76. /// <param name="level">Block size acts as compression level (1 to 9) with 1 giving
  77. /// the lowest compression and 9 the highest.</param>
  78. public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level)
  79. {
  80. if (inStream == null || outStream == null) {
  81. throw new Exception("Null Stream");
  82. }
  83. try {
  84. using (BZip2OutputStream bzipOutput = new BZip2OutputStream(outStream, level)) {
  85. bzipOutput.IsStreamOwner = isStreamOwner;
  86. Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]);
  87. }
  88. } finally {
  89. if (isStreamOwner) {
  90. // outStream is closed by the BZip2OutputStream if stream owner
  91. inStream.Close();
  92. }
  93. }
  94. }
  95. }
  96. }
  97. #endif