GZipOutputStream.cs 5.6 KB

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