comparison BZip2/BZip2.cs @ 1:94e25b786321

Re #311: can't read ZIP file packed by Linux app Archive Manager/File Roller Initial commit of clean SharpZipLib 0860 source. Only change is build paths.
author IBBoard <dev@ibboard.co.uk>
date Sat, 30 Oct 2010 14:03:17 +0000
parents
children
comparison
equal deleted inserted replaced
0:d16ef17fa7bb 1:94e25b786321
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
36 // Suppress this in CF and 1.1, not needed. Static classes introduced in C# version 2.0
37 #if !NETCF_2_0 && !NET_1_1
38
39 using System;
40 using System.IO;
41
42 namespace ICSharpCode.SharpZipLib.BZip2 {
43
44 /// <summary>
45 /// An example class to demonstrate compression and decompression of BZip2 streams.
46 /// </summary>
47 public static class BZip2
48 {
49 /// <summary>
50 /// Decompress the <paramref name="inStream">input</paramref> writing
51 /// uncompressed data to the <paramref name="outStream">output stream</paramref>
52 /// </summary>
53 /// <param name="inStream">The readable stream containing data to decompress.</param>
54 /// <param name="outStream">The output stream to receive the decompressed data.</param>
55 /// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
56 public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner)
57 {
58 if (inStream == null || outStream == null) {
59 throw new Exception("Null Stream");
60 }
61
62 try {
63 using (BZip2InputStream bzipInput = new BZip2InputStream(inStream)) {
64 bzipInput.IsStreamOwner = isStreamOwner;
65 Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]);
66 }
67 } finally {
68 if (isStreamOwner) {
69 // inStream is closed by the BZip2InputStream if stream owner
70 outStream.Close();
71 }
72 }
73 }
74
75 /// <summary>
76 /// Compress the <paramref name="inStream">input stream</paramref> sending
77 /// result data to <paramref name="outStream">output stream</paramref>
78 /// </summary>
79 /// <param name="inStream">The readable stream to compress.</param>
80 /// <param name="outStream">The output stream to receive the compressed data.</param>
81 /// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
82 /// <param name="level">Block size acts as compression level (1 to 9) with 1 giving
83 /// the lowest compression and 9 the highest.</param>
84 public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level)
85 {
86 if (inStream == null || outStream == null) {
87 throw new Exception("Null Stream");
88 }
89
90 try {
91 using (BZip2OutputStream bzipOutput = new BZip2OutputStream(outStream, level)) {
92 bzipOutput.IsStreamOwner = isStreamOwner;
93 Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]);
94 }
95 } finally {
96 if (isStreamOwner) {
97 // outStream is closed by the BZip2OutputStream if stream owner
98 inStream.Close();
99 }
100 }
101 }
102
103 }
104 }
105 #endif