|
| 1 | +// GZip.cs |
| 2 | +// |
| 3 | +// Copyright © 2000-2016 AlphaSierraPapa for the SharpZipLib Team |
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 | + |
| 36 | +using System; |
| 37 | +using System.IO; |
| 38 | + |
| 39 | +namespace ICSharpCode.SharpZipLib.GZip |
| 40 | +{ |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// An example class to demonstrate compression and decompression of GZip streams. |
| 44 | + /// </summary> |
| 45 | + public static class GZip |
| 46 | + { |
| 47 | + /// <summary> |
| 48 | + /// Decompress the <paramref name="inStream">input</paramref> writing |
| 49 | + /// uncompressed data to the <paramref name="outStream">output stream</paramref> |
| 50 | + /// </summary> |
| 51 | + /// <param name="inStream">The readable stream containing data to decompress.</param> |
| 52 | + /// <param name="outStream">The output stream to receive the decompressed data.</param> |
| 53 | + /// <param name="isStreamOwner">Both streams are closed on completion if true.</param> |
| 54 | + public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner) |
| 55 | + { |
| 56 | + if (inStream == null || outStream == null) { |
| 57 | + throw new Exception("Null Stream"); |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + using (GZipInputStream bzipInput = new GZipInputStream(inStream)) { |
| 62 | + bzipInput.IsStreamOwner = isStreamOwner; |
| 63 | + Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]); |
| 64 | + } |
| 65 | + } finally { |
| 66 | + if (isStreamOwner) { |
| 67 | + // inStream is closed by the GZipInputStream if stream owner |
| 68 | + outStream.Close(); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Compress the <paramref name="inStream">input stream</paramref> sending |
| 75 | + /// result data to <paramref name="outStream">output stream</paramref> |
| 76 | + /// </summary> |
| 77 | + /// <param name="inStream">The readable stream to compress.</param> |
| 78 | + /// <param name="outStream">The output stream to receive the compressed data.</param> |
| 79 | + /// <param name="isStreamOwner">Both streams are closed on completion if true.</param> |
| 80 | + /// <param name="level">Block size acts as compression level (1 to 9) with 1 giving |
| 81 | + /// the lowest compression and 9 the highest.</param> |
| 82 | + public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level) |
| 83 | + { |
| 84 | + if (inStream == null || outStream == null) { |
| 85 | + throw new Exception("Null Stream"); |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + using (GZipOutputStream bzipOutput = new GZipOutputStream(outStream, level)) { |
| 90 | + bzipOutput.IsStreamOwner = isStreamOwner; |
| 91 | + Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]); |
| 92 | + } |
| 93 | + } finally { |
| 94 | + if (isStreamOwner) { |
| 95 | + // outStream is closed by the GZipOutputStream if stream owner |
| 96 | + inStream.Close(); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | +} |
0 commit comments