(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / ICSharpCode.SharpZipLib / ICSharpCode.SharpZipLib / GZip / GZipOutputStream.cs
blobb150e66d5c2db1e826b7b4a1343170a68cc1caa2
1 // GzipOutputStream.cs
2 // Copyright (C) 2001 Mike Krueger
3 //
4 // This file was translated from java, it was part of the GNU Classpath
5 // Copyright (C) 2001 Free Software Foundation, Inc.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 // Linking this library statically or dynamically with other modules is
22 // making a combined work based on this library. Thus, the terms and
23 // conditions of the GNU General Public License cover the whole
24 // combination.
25 //
26 // As a special exception, the copyright holders of this library give you
27 // permission to link this library with independent modules to produce an
28 // executable, regardless of the license terms of these independent
29 // modules, and to copy and distribute the resulting executable under
30 // terms of your choice, provided that you also meet, for each linked
31 // independent module, the terms and conditions of the license of that
32 // module. An independent module is a module which is not derived from
33 // or based on this library. If you modify this library, you may extend
34 // this exception to your version of the library, but you are not
35 // obligated to do so. If you do not wish to do so, delete this
36 // exception statement from your version.
38 using System;
39 using System.IO;
41 using ICSharpCode.SharpZipLib.Checksums;
42 using ICSharpCode.SharpZipLib.Zip.Compression;
43 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
45 namespace ICSharpCode.SharpZipLib.GZip
48 /// <summary>
49 /// This filter stream is used to compress a stream into a "GZIP" stream.
50 /// The "GZIP" format is described in RFC 1952.
51 ///
52 /// author of the original java version : John Leuner
53 /// </summary>
54 /// <example> This sample shows how to gzip a file
55 /// <code>
56 /// using System;
57 /// using System.IO;
58 ///
59 /// using ICSharpCode.SharpZipLib.GZip; // -jr- corrected
60 ///
61 /// class MainClass
62 /// {
63 /// public static void Main(string[] args)
64 /// {
65 /// Stream s = new GZipOutputStream(File.Create(args[0] + ".gz"));
66 /// FileStream fs = File.OpenRead(args[0]);
67 /// byte[] writeData = new byte[fs.Length];
68 /// fs.Read(writeData, 0, (int)fs.Length);
69 /// s.Write(writeData, 0, writeData.Length);
70 /// s.Close();
71 /// }
72 /// }
73 /// </code>
74 /// </example>
75 public class GZipOutputStream : DeflaterOutputStream
77 //Variables
79 /// <summary>
80 /// CRC-32 value for uncompressed data
81 /// </summary>
82 protected Crc32 crc = new Crc32();
84 // Constructors
86 /// <summary>
87 /// Creates a GzipOutputStream with the default buffer size
88 /// </summary>
89 /// <param name="baseOutputStream">
90 /// The stream to read data (to be compressed) from
91 /// </param>
92 public GZipOutputStream(Stream baseOutputStream) : this(baseOutputStream, 4096)
96 /// <summary>
97 /// Creates a GZIPOutputStream with the specified buffer size
98 /// </summary>
99 /// <param name="baseOutputStream">
100 /// The stream to read data (to be compressed) from
101 /// </param>
102 /// <param name="size">
103 /// Size of the buffer to use
104 /// </param>
105 public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size)
107 WriteHeader();
108 // System.err.println("wrote GZIP header (" + gzipHeader.length + " bytes )");
111 void WriteHeader()
113 int mod_time = (int)(DateTime.Now.Ticks / 10000L); // Ticks give back 100ns intervals
114 byte[] gzipHeader = {
115 /* The two magic bytes */
116 (byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) GZipConstants.GZIP_MAGIC,
118 /* The compression type */
119 (byte) Deflater.DEFLATED,
121 /* The flags (not set) */
124 /* The modification time */
125 (byte) mod_time, (byte) (mod_time >> 8),
126 (byte) (mod_time >> 16), (byte) (mod_time >> 24),
128 /* The extra flags */
131 /* The OS type (unknown) */
132 (byte) 255
134 baseOutputStream.Write(gzipHeader, 0, gzipHeader.Length);
137 public override void Write(byte[] buf, int off, int len)
139 crc.Update(buf, off, len);
140 base.Write(buf, off, len);
143 /// <summary>
144 /// Writes remaining compressed output data to the output stream
145 /// and closes it.
146 /// </summary>
147 public override void Close()
149 Finish();
150 baseOutputStream.Close();
153 public override void Finish()
155 base.Finish();
157 int totalin = def.TotalIn;
158 int crcval = (int) (crc.Value & 0xffffffff);
160 // System.err.println("CRC val is " + Integer.toHexString( crcval ) + " and length " + Integer.toHexString(totalin));
162 byte[] gzipFooter = {
163 (byte) crcval, (byte) (crcval >> 8),
164 (byte) (crcval >> 16), (byte) (crcval >> 24),
166 (byte) totalin, (byte) (totalin >> 8),
167 (byte) (totalin >> 16), (byte) (totalin >> 24)
170 baseOutputStream.Write(gzipFooter, 0, gzipFooter.Length);
171 // System.err.println("wrote GZIP trailer (" + gzipFooter.length + " bytes )");