123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using System;
- using System.IO;
- using CommonMPQ.SharpZipLib.Checksums;
- using CommonMPQ.SharpZipLib.Zip.Compression;
- using CommonMPQ.SharpZipLib.Zip.Compression.Streams;
- namespace CommonMPQ.SharpZipLib.GZip
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class GZipOutputStream : DeflaterOutputStream
- {
- enum OutputState
- {
- Header,
- Footer,
- Finished,
- Closed,
- };
- #region Instance Fields
-
-
-
- protected Crc32 crc = new Crc32();
- OutputState state_ = OutputState.Header;
- #endregion
- #region Constructors
-
-
-
-
-
-
- public GZipOutputStream(Stream baseOutputStream)
- : this(baseOutputStream, 4096)
- {
- }
-
-
-
-
-
-
-
-
-
-
- public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size)
- {
- }
- #endregion
-
- #region Public API
-
-
-
-
-
-
-
-
-
- public void SetLevel(int level)
- {
- if (level < Deflater.BEST_SPEED) {
- throw new ArgumentOutOfRangeException("level");
- }
- deflater_.SetLevel(level);
- }
-
-
-
-
-
- public int GetLevel()
- {
- return deflater_.GetLevel();
- }
- #endregion
-
- #region Stream overrides
-
-
-
-
-
-
- public override void Write(byte[] buffer, int offset, int count)
- {
- if ( state_ == OutputState.Header ) {
- WriteHeader();
- }
- if( state_!=OutputState.Footer )
- {
- throw new InvalidOperationException("Write not permitted in current state");
- }
- crc.Update(buffer, offset, count);
- base.Write(buffer, offset, count);
- }
-
-
-
-
-
- public override void Close()
- {
- try {
- Finish();
- }
- finally {
- if ( state_ != OutputState.Closed ) {
- state_ = OutputState.Closed;
- if( IsStreamOwner ) {
- baseOutputStream_.Close();
- }
- }
- }
- }
- #endregion
-
- #region DeflaterOutputStream overrides
-
-
-
- public override void Finish()
- {
-
- if ( state_ == OutputState.Header ) {
- WriteHeader();
- }
- if( state_ == OutputState.Footer)
- {
- state_=OutputState.Finished;
- base.Finish();
- uint totalin=(uint)(deflater_.TotalIn&0xffffffff);
- uint crcval=(uint)(crc.Value&0xffffffff);
- byte[] gzipFooter;
- unchecked
- {
- gzipFooter=new byte[] {
- (byte) crcval, (byte) (crcval >> 8),
- (byte) (crcval >> 16), (byte) (crcval >> 24),
-
- (byte) totalin, (byte) (totalin >> 8),
- (byte) (totalin >> 16), (byte) (totalin >> 24)
- };
- }
- baseOutputStream_.Write(gzipFooter, 0, gzipFooter.Length);
- }
- }
- #endregion
-
- #region Support Routines
- void WriteHeader()
- {
- if ( state_ == OutputState.Header )
- {
- state_=OutputState.Footer;
- int mod_time = (int)((DateTime.Now.Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000L);
- byte[] gzipHeader = {
-
- (byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) (GZipConstants.GZIP_MAGIC & 0xff),
-
- (byte) Deflater.DEFLATED,
-
- 0,
-
- (byte) mod_time, (byte) (mod_time >> 8),
- (byte) (mod_time >> 16), (byte) (mod_time >> 24),
-
- 0,
-
- (byte) 255
- };
- baseOutputStream_.Write(gzipHeader, 0, gzipHeader.Length);
- }
- }
- #endregion
- }
- }
|