温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:X3BLOG 单用户1.0 build80707(ACCESS)源代码
当前文件路径:x3blogAccessBuild80707/SharpZipLib/GZip/GzipOutputStream.cs

1// GzipOutputStream.cs 2
// 3
// Copyright (C) 2001 Mike Krueger 4
// 5
// This file was translated from java, it was part of the GNU Classpath 6
// Copyright (C) 2001 Free Software Foundation, Inc. 7
// 8
// This program is free software; you can redistribute it and/or 9
// modify it under the terms of the GNU General Public License 10
// as published by the Free Software Foundation; either version 2 11
// of the License, or (at your option) any later version. 12
// 13
// This program is distributed in the hope that it will be useful, 14
// but WITHOUT ANY WARRANTY; without even the implied warranty of 15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16
// GNU General Public License for more details. 17
// 18
// You should have received a copy of the GNU General Public License 19
// along with this program; if not, write to the Free Software 20
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 21
// 22
// Linking this library statically or dynamically with other modules is 23
// making a combined work based on this library. Thus, the terms and 24
// conditions of the GNU General Public License cover the whole 25
// combination. 26
// 27
// As a special exception, the copyright holders of this library give you 28
// permission to link this library with independent modules to produce an 29
// executable, regardless of the license terms of these independent 30
// modules, and to copy and distribute the resulting executable under 31
// terms of your choice, provided that you also meet, for each linked 32
// independent module, the terms and conditions of the license of that 33
// module. An independent module is a module which is not derived from 34
// or based on this library. If you modify this library, you may extend 35
// this exception to your version of the library, but you are not 36
// obligated to do so. If you do not wish to do so, delete this 37
// exception statement from your version. 38
39
using System; 40
using System.IO; 41
42
using ICSharpCode.SharpZipLib.Checksums; 43
using ICSharpCode.SharpZipLib.Zip.Compression; 44
using ICSharpCode.SharpZipLib.Zip.Compression.Streams; 45
46
namespace ICSharpCode.SharpZipLib.GZip 47
{ 48
49
/// <summary> 50
/// This filter stream is used to compress a stream into a "GZIP" stream. 51
/// The "GZIP" format is described in RFC 1952. 52
/// 53
/// author of the original java version : John Leuner 54
/// </summary> 55
/// <example> This sample shows how to gzip a file 56
/// <code> 57
/// using System; 58
/// using System.IO; 59
/// 60
/// using ICSharpCode.SharpZipLib.GZip; 61
/// 62
/// class MainClass 63
/// { 64
/// public static void Main(string[] args) 65
/// { 66
/// Stream s = new GZipOutputStream(File.Create(args[0] + ".gz")); 67
/// FileStream fs = File.OpenRead(args[0]); 68
/// byte[] writeData = new byte[fs.Length]; 69
/// fs.Read(writeData, 0, (int)fs.Length); 70
/// s.Write(writeData, 0, writeData.Length); 71
/// s.Close(); 72
/// } 73
/// } 74
/// </code> 75
/// </example> 76
public class GZipOutputStream : DeflaterOutputStream 77
{ 78
/// <summary> 79
/// CRC-32 value for uncompressed data 80
/// </summary> 81
protected Crc32 crc = new Crc32(); 82
83
/// <summary> 84
/// Creates a GzipOutputStream with the default buffer size 85
/// </summary> 86
/// <param name="baseOutputStream"> 87
/// The stream to read data (to be compressed) from 88
/// </param> 89
public GZipOutputStream(Stream baseOutputStream) : this(baseOutputStream, 4096) 90
{ 91
} 92
93
/// <summary> 94
/// Creates a GZipOutputStream with the specified buffer size 95
/// </summary> 96
/// <param name="baseOutputStream"> 97
/// The stream to read data (to be compressed) from 98
/// </param> 99
/// <param name="size"> 100
/// Size of the buffer to use 101
/// </param> 102
public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size) 103
{ 104
WriteHeader(); 105
} 106
107
void WriteHeader() 108
{ 109
int mod_time = (int)(DateTime.Now.Ticks / 10000L); // Ticks give back 100ns intervals 110
byte[] gzipHeader = { 111
/* The two magic bytes */ 112
(byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) GZipConstants.GZIP_MAGIC, 113
114
/* The compression type */ 115
(byte) Deflater.DEFLATED, 116
117
/* The flags (not set) */ 118
0, 119
120
/* The modification time */ 121
(byte) mod_time, (byte) (mod_time >> 8), 122
(byte) (mod_time >> 16), (byte) (mod_time >> 24), 123
124
/* The extra flags */ 125
0, 126
127
/* The OS type (unknown) */ 128
(byte) 255 129
}; 130
baseOutputStream.Write(gzipHeader, 0, gzipHeader.Length); 131
} 132
133
/// <summary> 134
/// Write given buffer to output updating crc 135
/// </summary> 136
/// <param name="buf">Buffer to write</param> 137
/// <param name="off">Offset of first byte in buf to write</param> 138
/// <param name="len">Number of bytes to write</param> 139
public override void Write(byte[] buf, int off, int len) 140
{ 141
crc.Update(buf, off, len); 142
base.Write(buf, off, len); 143
} 144
145
/// <summary> 146
/// Writes remaining compressed output data to the output stream 147
/// and closes it. 148
/// </summary> 149
public override void Close() 150
{ 151
Finish(); 152
153
if ( IsStreamOwner ) { 154
baseOutputStream.Close(); 155
} 156
} 157
158
/// <summary> 159
/// Sets the active compression level (1-9). The new level will be activated 160
/// immediately. 161
/// </summary> 162
/// <exception cref="ArgumentOutOfRangeException"> 163
/// Level specified is not supported. 164
/// </exception> 165
/// <see cref="Deflater"/> 166
public void SetLevel(int level) 167
{ 168
if (level < Deflater.BEST_SPEED) { 169
throw new ArgumentOutOfRangeException("level"); 170
} 171
def.SetLevel(level); 172
} 173
174
/// <summary> 175
/// Get the current compression level. 176
/// </summary> 177
/// <returns>The current compression level.</returns> 178
public int GetLevel() 179
{ 180
return def.GetLevel(); 181
} 182
183
/// <summary> 184
/// Finish compression and write any footer information required to stream 185
/// </summary> 186
public override void Finish() 187
{ 188
base.Finish(); 189
190
int totalin = def.TotalIn; 191
int crcval = (int) (crc.Value & 0xffffffff); 192
193
// System.err.println("CRC val is " + Integer.toHexString( crcval ) + " and length " + Integer.toHexString(totalin)); 194
195
byte[] gzipFooter = { 196
(byte) crcval, (byte) (crcval >> 8), 197
(byte) (crcval >> 16), (byte) (crcval >> 24), 198
199
(byte) totalin, (byte) (totalin >> 8), 200
(byte) (totalin >> 16), (byte) (totalin >> 24) 201
}; 202
203
baseOutputStream.Write(gzipFooter, 0, gzipFooter.Length); 204
// System.err.println("wrote GZIP trailer (" + gzipFooter.length + " bytes )"); 205
} 206
} 207
} 208





}