GZipOutputStream.cs 5.6 KB

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