| 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. | |
| 11: | // | |
| 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. | |
| 16: | // | |
| 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. | |
| 20: | // | |
| 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. | |
| 37: | ||
| 38: | using System; | |
| 39: | using System.IO; | |
| 40: | ||
| 41: | using ICSharpCode.SharpZipLib.Checksums; | |
| 42: | using ICSharpCode.SharpZipLib.Zip.Compression; | |
| 43: | using ICSharpCode.SharpZipLib.Zip.Compression.Streams; | |
| 44: | ||
| 45: | namespace ICSharpCode.SharpZipLib.GZip { | |
| 46: | ||
| 47: | /// <summary> | |
| 48: | /// This filter stream is used to compress a stream into a "GZIP" stream. | |
| 49: | /// The "GZIP" format is described in RFC 1952. | |
| 50: | /// | |
| 51: | /// author of the original java version : John Leuner | |
| 52: | /// </summary> | |
| 53: | /// <example> This sample shows how to gzip a file | |
| 54: | /// <code> | |
| 55: | /// using System; | |
| 56: | /// using System.IO; | |
| 57: | /// | |
| 58: | /// using NZlib.GZip; | |
| 59: | /// | |
| 60: | /// class MainClass | |
| 61: | /// { | |
| 62: | /// public static void Main(string[] args) | |
| 63: | /// { | |
| 64: | /// Stream s = new GZipOutputStream(File.Create(args[0] + ".gz")); | |
| 65: | /// FileStream fs = File.OpenRead(args[0]); | |
| 66: | /// byte[] writeData = new byte[fs.Length]; | |
| 67: | /// fs.Read(writeData, 0, (int)fs.Length); | |
| 68: | /// s.Write(writeData, 0, writeData.Length); | |
| 69: | /// s.Close(); | |
| 70: | /// } | |
| 71: | /// } | |
| 72: | /// </code> | |
| 73: | /// </example> | |
| 74: | public class GZipOutputStream : DeflaterOutputStream | |
| 75: | { | |
| 76: | //Variables | |
| 77: | ||
| 78: | /// <summary> | |
| 79: | /// CRC-32 value for uncompressed data | |
| 80: | /// </summary> | |
| 81: | protected Crc32 crc = new Crc32(); | |
| 82: | ||
| 83: | // Constructors | |
| 84: | ||
| 85: | /// <summary> | |
| 86: | /// Creates a GzipOutputStream with the default buffer size | |
| 87: | /// </summary> | |
| 88: | /// <param name="baseOutputStream"> | |
| 89: | /// The stream to read data (to be compressed) from | |
| 90: | /// </param> | |
| 91: | public GZipOutputStream(Stream baseOutputStream) : this(baseOutputStream, 4096) | |
| 92: | { | |
| 93: | } | |
| 94: | ||
| 95: | /// <summary> | |
| 96: | /// Creates a GZIPOutputStream with the specified buffer size | |
| 97: | /// </summary> | |
| 98: | /// <param name="baseOutputStream"> | |
| 99: | /// The stream to read data (to be compressed) from | |
| 100: | /// </param> | |
| 101: | /// <param name="size"> | |
| 102: | /// Size of the buffer to use | |
| 103: | /// </param> | |
| 104: | public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size) | |
| 105: | { | |
| 106: | // TODO : find out correctness, orgininally this was : (int) (System.currentTimeMillis() / 1000L); | |
| 107: | int mod_time = (int)(DateTime.Now.Ticks / 10000L); // Ticks give back 100ns intervals | |
| 108: | byte[] gzipHeader = { | |
| 109: | /* The two magic bytes */ | |
| 110: | (byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) GZipConstants.GZIP_MAGIC, | |
| 111: | ||
| 112: | /* The compression type */ | |
| 113: | (byte) Deflater.DEFLATED, | |
| 114: | ||
| 115: | /* The flags (not set) */ | |
| 116: | 0, | |
| 117: | ||
| 118: | /* The modification time */ | |
| 119: | (byte) mod_time, (byte) (mod_time >> 8), | |
| 120: | (byte) (mod_time >> 16), (byte) (mod_time >> 24), | |
| 121: | ||
| 122: | /* The extra flags */ | |
| 123: | 0, | |
| 124: | ||
| 125: | /* The OS type (unknown) */ | |
| 126: | (byte) 255 | |
| 127: | }; | |
| 128: | ||
| 129: | baseOutputStream.Write(gzipHeader, 0, gzipHeader.Length); | |
| 130: | // System.err.println("wrote GZIP header (" + gzipHeader.length + " bytes )"); | |
| 131: | } | |
| 132: | ||
| 133: | public override void Write(byte[] buf, int off, int len) | |
| 134: | { | |
| 135: | crc.Update(buf, off, len); | |
| 136: | base.Write(buf, off, len); | |
| 137: | } | |
| 138: | ||
| 139: | /// <summary> | |
| 140: | /// Writes remaining compressed output data to the output stream | |
| 141: | /// and closes it. | |
| 142: | /// </summary> | |
| 143: | public override void Close() | |
| 144: | { | |
| 145: | Finish(); | |
| 146: | baseOutputStream.Close(); | |
| 147: | } | |
| 148: | ||
| 149: | public override void Finish() | |
| 150: | { | |
| 151: | base.Finish(); | |
| 152: | ||
| 153: | int totalin = def.TotalIn; | |
| 154: | int crcval = (int) (crc.Value & 0xffffffff); | |
| 155: | ||
| 156: | // System.err.println("CRC val is " + Integer.toHexString( crcval ) + " and length " + Integer.toHexString(totalin)); | |
| 157: | ||
| 158: | byte[] gzipFooter = { | |
| 159: | (byte) crcval, (byte) (crcval >> 8), | |
| 160: | (byte) (crcval >> 16), (byte) (crcval >> 24), | |
| 161: | ||
| 162: | (byte) totalin, (byte) (totalin >> 8), | |
| 163: | (byte) (totalin >> 16), (byte) (totalin >> 24) | |
| 164: | }; | |
| 165: | ||
| 166: | baseOutputStream.Write(gzipFooter, 0, gzipFooter.Length); | |
| 167: | // System.err.println("wrote GZIP trailer (" + gzipFooter.length + " bytes )"); | |
| 168: | } | |
| 169: | } | |
| 170: | } |
This page was automatically generated by SharpDevelop.