Statistics
| Branch: | Revision:

root / trunk / hammock / src / net35 / ICSharpCode.SharpZipLib.Silverlight / BZip2 / BZip2.cs @ 0eea575a

History | View | Annotate | Download (4.8 kB)

1
// BZip2.cs
2
//
3
// Copyright (C) 2001 Mike Krueger
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
using System;
37
using System.IO;
38

    
39
namespace ICSharpCode.SharpZipLib.Silverlight.BZip2
40
{
41
    /// <summary>
42
    /// A helper class to simplify compressing and decompressing streams.
43
    /// </summary>
44
    public static class BZip2
45
    {
46
        /// <summary>
47
        /// Decompress <paramref name="inStream">input</paramref> writing 
48
        /// decompressed data to the <paramref name="outStream">output stream</paramref>
49
        /// </summary>
50
        /// <param name="inStream">The stream containing data to decompress.</param>
51
        /// <param name="outStream">The stream to write decompressed data to.</param>
52
        /// <remarks>Both streams are closed on completion</remarks>
53
        public static void Decompress(Stream inStream, Stream outStream) 
54
        {
55
            if ( inStream == null ) {
56
                throw new ArgumentNullException("inStream");
57
            }
58
			
59
            if ( outStream == null ) {
60
                throw new ArgumentNullException("outStream");
61
            }
62
			
63
            using ( outStream ) {
64
                using ( var bzis = new BZip2InputStream(inStream) ) {
65
                    var ch = bzis.ReadByte();
66
                    while (ch != -1) {
67
                        outStream.WriteByte((byte)ch);
68
                        ch = bzis.ReadByte();
69
                    }
70
                }
71
            }
72
        }
73
		
74
        /// <summary>
75
        /// Compress <paramref name="inStream">input stream</paramref> sending 
76
        /// result to <paramref name="outStream">output stream</paramref>
77
        /// </summary>
78
        /// <param name="inStream">The stream to compress.</param>
79
        /// <param name="outStream">The stream to write compressed data to.</param>
80
        /// <param name="blockSize">The block size to use.</param>
81
        /// <remarks>Both streams are closed on completion</remarks>
82
        public static void Compress(Stream inStream, Stream outStream, int blockSize) 
83
        {			
84
            if ( inStream == null ) {
85
                throw new ArgumentNullException("inStream");
86
            }
87
			
88
            if ( outStream == null ) {
89
                throw new ArgumentNullException("outStream");
90
            }
91
			
92
            using ( inStream ) {
93
                using (var bzos = new BZip2OutputStream(outStream, blockSize)) {
94
                    var ch = inStream.ReadByte();
95
                    while (ch != -1) {
96
                        bzos.WriteByte((byte)ch);
97
                        ch = inStream.ReadByte();
98
                    }
99
                }
100
            }
101
        }
102
    }
103
}
104

    
105
/* derived from a file which contained this license :
106
 * Copyright (c) 1999-2001 Keiron Liddle, Aftex Software
107
 *
108
 * This library is free software; you can redistribute it and/or
109
 * modify it under the terms of the GNU Lesser General Public
110
 * License as published by the Free Software Foundation; either
111
 * version 2.1 of the License, or (at your option) any later version.
112
 *
113
 * This library is distributed in the hope that it will be useful,
114
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
115
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116
 * Lesser General Public License for more details.
117
 *
118
*/