| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using ICSharpCode.SharpZipLib.Checksum; |
| | | 4 | | using ICSharpCode.SharpZipLib.Zip.Compression; |
| | | 5 | | using ICSharpCode.SharpZipLib.Zip.Compression.Streams; |
| | | 6 | | |
| | | 7 | | namespace ICSharpCode.SharpZipLib.GZip |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// This filter stream is used to compress a stream into a "GZIP" stream. |
| | | 11 | | /// The "GZIP" format is described in RFC 1952. |
| | | 12 | | /// |
| | | 13 | | /// author of the original java version : John Leuner |
| | | 14 | | /// </summary> |
| | | 15 | | /// <example> This sample shows how to gzip a file |
| | | 16 | | /// <code> |
| | | 17 | | /// using System; |
| | | 18 | | /// using System.IO; |
| | | 19 | | /// |
| | | 20 | | /// using ICSharpCode.SharpZipLib.GZip; |
| | | 21 | | /// using ICSharpCode.SharpZipLib.Core; |
| | | 22 | | /// |
| | | 23 | | /// class MainClass |
| | | 24 | | /// { |
| | | 25 | | /// public static void Main(string[] args) |
| | | 26 | | /// { |
| | | 27 | | /// using (Stream s = new GZipOutputStream(File.Create(args[0] + ".gz"))) |
| | | 28 | | /// using (FileStream fs = File.OpenRead(args[0])) { |
| | | 29 | | /// byte[] writeData = new byte[4096]; |
| | | 30 | | /// Streamutils.Copy(s, fs, writeData); |
| | | 31 | | /// } |
| | | 32 | | /// } |
| | | 33 | | /// } |
| | | 34 | | /// } |
| | | 35 | | /// </code> |
| | | 36 | | /// </example> |
| | | 37 | | public class GZipOutputStream : DeflaterOutputStream |
| | | 38 | | { |
| | | 39 | | enum OutputState |
| | | 40 | | { |
| | | 41 | | Header, |
| | | 42 | | Footer, |
| | | 43 | | Finished, |
| | | 44 | | Closed, |
| | | 45 | | }; |
| | | 46 | | |
| | | 47 | | #region Instance Fields |
| | | 48 | | /// <summary> |
| | | 49 | | /// CRC-32 value for uncompressed data |
| | | 50 | | /// </summary> |
| | 9 | 51 | | protected Crc32 crc = new Crc32(); |
| | | 52 | | OutputState state_ = OutputState.Header; |
| | | 53 | | #endregion |
| | | 54 | | |
| | | 55 | | #region Constructors |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates a GzipOutputStream with the default buffer size |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="baseOutputStream"> |
| | | 60 | | /// The stream to read data (to be compressed) from |
| | | 61 | | /// </param> |
| | | 62 | | public GZipOutputStream(Stream baseOutputStream) |
| | 9 | 63 | | : this(baseOutputStream, 4096) |
| | | 64 | | { |
| | 9 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Creates a GZipOutputStream with the specified buffer size |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="baseOutputStream"> |
| | | 71 | | /// The stream to read data (to be compressed) from |
| | | 72 | | /// </param> |
| | | 73 | | /// <param name="size"> |
| | | 74 | | /// Size of the buffer to use |
| | | 75 | | /// </param> |
| | 9 | 76 | | public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_CO |
| | | 77 | | { |
| | 9 | 78 | | } |
| | | 79 | | #endregion |
| | | 80 | | |
| | | 81 | | #region Destructor |
| | | 82 | | /// <summary> |
| | | 83 | | /// Ensures that resources are freed and other cleanup operations |
| | | 84 | | /// are performed when the garbage collector reclaims the GZipOutputStream. |
| | | 85 | | /// </summary> |
| | | 86 | | ~GZipOutputStream() |
| | | 87 | | { |
| | 9 | 88 | | Dispose(false); |
| | 18 | 89 | | } |
| | | 90 | | #endregion |
| | | 91 | | |
| | | 92 | | #region Public API |
| | | 93 | | /// <summary> |
| | | 94 | | /// Sets the active compression level (1-9). The new level will be activated |
| | | 95 | | /// immediately. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="level">The compression level to set.</param> |
| | | 98 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 99 | | /// Level specified is not supported. |
| | | 100 | | /// </exception> |
| | | 101 | | /// <see cref="Deflater"/> |
| | | 102 | | public void SetLevel(int level) |
| | | 103 | | { |
| | 0 | 104 | | if (level < Deflater.BEST_SPEED) { |
| | 0 | 105 | | throw new ArgumentOutOfRangeException(nameof(level)); |
| | | 106 | | } |
| | 0 | 107 | | deflater_.SetLevel(level); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Get the current compression level. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <returns>The current compression level.</returns> |
| | | 114 | | public int GetLevel() |
| | | 115 | | { |
| | 0 | 116 | | return deflater_.GetLevel(); |
| | | 117 | | } |
| | | 118 | | #endregion |
| | | 119 | | |
| | | 120 | | #region Stream overrides |
| | | 121 | | /// <summary> |
| | | 122 | | /// Write given buffer to output updating crc |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="buffer">Buffer to write</param> |
| | | 125 | | /// <param name="offset">Offset of first byte in buf to write</param> |
| | | 126 | | /// <param name="count">Number of bytes to write</param> |
| | | 127 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 128 | | { |
| | 3 | 129 | | if (state_ == OutputState.Header) { |
| | 1 | 130 | | WriteHeader(); |
| | | 131 | | } |
| | | 132 | | |
| | 3 | 133 | | if (state_ != OutputState.Footer) { |
| | 2 | 134 | | throw new InvalidOperationException("Write not permitted in current state"); |
| | | 135 | | } |
| | | 136 | | |
| | 1 | 137 | | crc.Update(buffer, offset, count); |
| | 1 | 138 | | base.Write(buffer, offset, count); |
| | 1 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Writes remaining compressed output data to the output stream |
| | | 143 | | /// and closes it. |
| | | 144 | | /// </summary> |
| | | 145 | | public override void Close() |
| | | 146 | | { |
| | | 147 | | try { |
| | 10 | 148 | | Finish(); |
| | 10 | 149 | | } finally { |
| | 10 | 150 | | if (state_ != OutputState.Closed) { |
| | 8 | 151 | | state_ = OutputState.Closed; |
| | 8 | 152 | | if (IsStreamOwner) { |
| | 7 | 153 | | baseOutputStream_.Close(); |
| | | 154 | | } |
| | | 155 | | } |
| | 10 | 156 | | } |
| | 10 | 157 | | } |
| | | 158 | | #endregion |
| | | 159 | | |
| | | 160 | | #region DeflaterOutputStream overrides |
| | | 161 | | /// <summary> |
| | | 162 | | /// Finish compression and write any footer information required to stream |
| | | 163 | | /// </summary> |
| | | 164 | | public override void Finish() |
| | | 165 | | { |
| | | 166 | | // If no data has been written a header should be added. |
| | 13 | 167 | | if (state_ == OutputState.Header) { |
| | 8 | 168 | | WriteHeader(); |
| | | 169 | | } |
| | | 170 | | |
| | 13 | 171 | | if (state_ == OutputState.Footer) { |
| | 9 | 172 | | state_ = OutputState.Finished; |
| | 9 | 173 | | base.Finish(); |
| | | 174 | | |
| | 9 | 175 | | var totalin = (uint)(deflater_.TotalIn & 0xffffffff); |
| | 9 | 176 | | var crcval = (uint)(crc.Value & 0xffffffff); |
| | | 177 | | |
| | | 178 | | byte[] gzipFooter; |
| | | 179 | | |
| | | 180 | | unchecked { |
| | 9 | 181 | | gzipFooter = new byte[] { |
| | 9 | 182 | | (byte) crcval, (byte) (crcval >> 8), |
| | 9 | 183 | | (byte) (crcval >> 16), (byte) (crcval >> 24), |
| | 9 | 184 | | |
| | 9 | 185 | | (byte) totalin, (byte) (totalin >> 8), |
| | 9 | 186 | | (byte) (totalin >> 16), (byte) (totalin >> 24) |
| | 9 | 187 | | }; |
| | | 188 | | } |
| | | 189 | | |
| | 9 | 190 | | baseOutputStream_.Write(gzipFooter, 0, gzipFooter.Length); |
| | | 191 | | } |
| | 13 | 192 | | } |
| | | 193 | | #endregion |
| | | 194 | | |
| | | 195 | | #region Support Routines |
| | | 196 | | void WriteHeader() |
| | | 197 | | { |
| | 9 | 198 | | if (state_ == OutputState.Header) { |
| | 9 | 199 | | state_ = OutputState.Footer; |
| | | 200 | | |
| | 9 | 201 | | var mod_time = (int)((DateTime.Now.Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000L); // Ticks give back 100 |
| | 9 | 202 | | byte[] gzipHeader = { |
| | 9 | 203 | | // The two magic bytes |
| | 9 | 204 | | (byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) (GZipConstants.GZIP_MAGIC & 0xff), |
| | 9 | 205 | | |
| | 9 | 206 | | // The compression type |
| | 9 | 207 | | (byte) Deflater.DEFLATED, |
| | 9 | 208 | | |
| | 9 | 209 | | // The flags (not set) |
| | 9 | 210 | | 0, |
| | 9 | 211 | | |
| | 9 | 212 | | // The modification time |
| | 9 | 213 | | (byte) mod_time, (byte) (mod_time >> 8), |
| | 9 | 214 | | (byte) (mod_time >> 16), (byte) (mod_time >> 24), |
| | 9 | 215 | | |
| | 9 | 216 | | // The extra flags |
| | 9 | 217 | | 0, |
| | 9 | 218 | | |
| | 9 | 219 | | // The OS type (unknown) |
| | 9 | 220 | | (byte) 255 |
| | 9 | 221 | | }; |
| | 9 | 222 | | baseOutputStream_.Write(gzipHeader, 0, gzipHeader.Length); |
| | | 223 | | } |
| | 9 | 224 | | } |
| | | 225 | | #endregion |
| | | 226 | | } |
| | | 227 | | } |