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

1// ZipInputStream.cs 2
// 3
// Copyright (C) 2001 Mike Krueger 4
// Copyright (C) 2004 John Reilly 5
// 6
// This file was translated from java, it was part of the GNU Classpath 7
// Copyright (C) 2001 Free Software Foundation, Inc. 8
// 9
// This program is free software; you can redistribute it and/or 10
// modify it under the terms of the GNU General Public License 11
// as published by the Free Software Foundation; either version 2 12
// of the License, or (at your option) any later version. 13
// 14
// This program is distributed in the hope that it will be useful, 15
// but WITHOUT ANY WARRANTY; without even the implied warranty of 16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17
// GNU General Public License for more details. 18
// 19
// You should have received a copy of the GNU General Public License 20
// along with this program; if not, write to the Free Software 21
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 22
// 23
// Linking this library statically or dynamically with other modules is 24
// making a combined work based on this library. Thus, the terms and 25
// conditions of the GNU General Public License cover the whole 26
// combination. 27
// 28
// As a special exception, the copyright holders of this library give you 29
// permission to link this library with independent modules to produce an 30
// executable, regardless of the license terms of these independent 31
// modules, and to copy and distribute the resulting executable under 32
// terms of your choice, provided that you also meet, for each linked 33
// independent module, the terms and conditions of the license of that 34
// module. An independent module is a module which is not derived from 35
// or based on this library. If you modify this library, you may extend 36
// this exception to your version of the library, but you are not 37
// obligated to do so. If you do not wish to do so, delete this 38
// exception statement from your version. 39
40
using System; 41
using System.Text; 42
using System.IO; 43
44
using ICSharpCode.SharpZipLib.Checksums; 45
using ICSharpCode.SharpZipLib.Zip.Compression; 46
using ICSharpCode.SharpZipLib.Zip.Compression.Streams; 47
using ICSharpCode.SharpZipLib.Encryption; 48
49
namespace ICSharpCode.SharpZipLib.Zip 50
{ 51
/// <summary> 52
/// This is an InflaterInputStream that reads the files baseInputStream an zip archive 53
/// one after another. It has a special method to get the zip entry of 54
/// the next file. The zip entry contains information about the file name 55
/// size, compressed size, Crc, etc. 56
/// It includes support for Stored and Deflated entries. 57
/// <br/> 58
/// <br/>Author of the original java version : Jochen Hoenicke 59
/// </summary> 60
/// 61
/// <example> This sample shows how to read a zip file 62
/// <code lang="C#"> 63
/// using System; 64
/// using System.Text; 65
/// using System.IO; 66
/// 67
/// using ICSharpCode.SharpZipLib.Zip; 68
/// 69
/// class MainClass 70
/// { 71
/// public static void Main(string[] args) 72
/// { 73
/// ZipInputStream s = new ZipInputStream(File.OpenRead(args[0])); 74
/// 75
/// ZipEntry theEntry; 76
/// while ((theEntry = s.GetNextEntry()) != null) { 77
/// int size = 2048; 78
/// byte[] data = new byte[2048]; 79
/// 80
/// Console.Write("Show contents (y/n) ?"); 81
/// if (Console.ReadLine() == "y") { 82
/// while (true) { 83
/// size = s.Read(data, 0, data.Length); 84
/// if (size > 0) { 85
/// Console.Write(new ASCIIEncoding().GetString(data, 0, size)); 86
/// } else { 87
/// break; 88
/// } 89
/// } 90
/// } 91
/// } 92
/// s.Close(); 93
/// } 94
/// } 95
/// </code> 96
/// </example> 97
public class ZipInputStream : InflaterInputStream 98
{ 99
// Delegate for reading bytes from a stream. 100
delegate int ReaderDelegate(byte[] b, int offset, int length); 101
102
/// <summary> 103
/// The current reader this instance. 104
/// </summary> 105
ReaderDelegate internalReader; 106
107
Crc32 crc = new Crc32(); 108
ZipEntry entry = null; 109
110
long size; 111
int method; 112
int flags; 113
string password = null; 114
115
/// <summary> 116
/// Creates a new Zip input stream, for reading a zip archive. 117
/// </summary> 118
public ZipInputStream(Stream baseInputStream) : base(baseInputStream, new Inflater(true)) 119
{ 120
internalReader = new ReaderDelegate(InitialRead); 121
} 122
123
124
/// <summary> 125
/// Optional password used for encryption when non-null 126
/// </summary> 127
public string Password 128
{ 129
get { 130
return password; 131
} 132
set { 133
password = value; 134
} 135
} 136
137
138
/// <summary> 139
/// Gets a value indicating if the entry can be decompressed 140
/// </summary> 141
/// <remarks> 142
/// The entry can only be decompressed if the library supports the zip features required to extract it. 143
/// See the <see cref="ZipEntry.Version">ZipEntry Version</see> property for more details. 144
/// </remarks> 145
public bool CanDecompressEntry { 146
get { 147
return entry != null && entry.Version <= ZipConstants.VERSION_MADE_BY; 148
} 149
} 150
151
/// <summary> 152
/// Advances to the next entry in the archive 153
/// </summary> 154
/// <returns> 155
/// The next <see cref="ZipEntry">entry</see> in the archive or null if there are no more entries. 156
/// </returns> 157
/// <remarks> 158
/// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called. 159
/// </remarks> 160
/// <exception cref="InvalidOperationException"> 161
/// Input stream is closed 162
/// </exception> 163
/// <exception cref="ZipException"> 164
/// Password is not set, password is invalid, compression method is invalid, 165
/// version required to extract is not supported 166
/// </exception> 167
public ZipEntry GetNextEntry() 168
{ 169
if (crc == null) { 170
throw new InvalidOperationException("Closed."); 171
} 172
173
if (entry != null) { 174
CloseEntry(); 175
} 176
177
int header = inputBuffer.ReadLeInt(); 178
179
if (header == ZipConstants.CENSIG || 180
header == ZipConstants.ENDSIG || 181
header == ZipConstants.CENDIGITALSIG || 182
header == ZipConstants.CENSIG64) { 183
// No more individual entries exist 184
Close(); 185
return null; 186
} 187
188
// -jr- 07-Dec-2003 Ignore spanning temporary signatures if found 189
// SPANNINGSIG is same as descriptor signature and is untested as yet. 190
if (header == ZipConstants.SPANTEMPSIG || header == ZipConstants.SPANNINGSIG) { 191
header = inputBuffer.ReadLeInt(); 192
} 193
194
if (header != ZipConstants.LOCSIG) { 195
throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header)); 196
} 197
198
short versionRequiredToExtract = (short)inputBuffer.ReadLeShort(); 199
200
flags = inputBuffer.ReadLeShort(); 201
method = inputBuffer.ReadLeShort(); 202
uint dostime = (uint)inputBuffer.ReadLeInt(); 203
int crc2 = inputBuffer.ReadLeInt(); 204
csize = inputBuffer.ReadLeInt(); 205
size = inputBuffer.ReadLeInt(); 206
int nameLen = inputBuffer.ReadLeShort(); 207
int extraLen = inputBuffer.ReadLeShort(); 208
209
bool isCrypted = (flags & 1) == 1; 210
211
byte[] buffer = new byte[nameLen]; 212
inputBuffer.ReadRawBuffer(buffer); 213
214
string name = ZipConstants.ConvertToString(buffer); 215
216
entry = new ZipEntry(name, versionRequiredToExtract); 217
entry.Flags = flags; 218
219
if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CRYPTO_HEADER_SIZE != size))) { 220
throw new ZipException("Stored, but compressed != uncompressed"); 221
} 222
223
if (method != (int)CompressionMethod.Stored && method != (int)CompressionMethod.Deflated) { 224
throw new ZipException("Unknown compression method " + method); 225
} 226
227
entry.CompressionMethod = (CompressionMethod)method; 228
229
if ((flags & 8) == 0) { 230
entry.Crc = crc2 & 0xFFFFFFFFL; 231
entry.Size = size & 0xFFFFFFFFL; 232
entry.CompressedSize = csize & 0xFFFFFFFFL; 233
} else { 234
235
// This allows for GNU, WinZip and possibly other archives, the PKZIP spec says these are zero 236
// under these circumstances. 237
if (crc2 != 0) { 238
entry.Crc = crc2 & 0xFFFFFFFFL; 239
} 240
241
if (size != 0) { 242
entry.Size = size & 0xFFFFFFFFL; 243
} 244
if (csize != 0) { 245
entry.CompressedSize = csize & 0xFFFFFFFFL; 246
} 247
} 248
249
entry.DosTime = dostime; 250
251
if (extraLen > 0) { 252
byte[] extra = new byte[extraLen]; 253
inputBuffer.ReadRawBuffer(extra); 254
entry.ExtraData = extra; 255
} 256
257
internalReader = new ReaderDelegate(InitialRead); 258
return entry; 259
} 260
261
// Read data descriptor at the end of compressed data. 262
void ReadDataDescriptor() 263
{ 264
if (inputBuffer.ReadLeInt() != ZipConstants.EXTSIG) { 265
throw new ZipException("Data descriptor signature not found"); 266
} 267
268
entry.Crc = inputBuffer.ReadLeInt() & 0xFFFFFFFFL; 269
csize = inputBuffer.ReadLeInt(); 270
size = inputBuffer.ReadLeInt(); 271
272
entry.Size = size & 0xFFFFFFFFL; 273
entry.CompressedSize = csize & 0xFFFFFFFFL; 274
} 275
276
/// <summary> 277
/// Closes the current zip entry and moves to the next one. 278
/// </summary> 279
/// <exception cref="InvalidOperationException"> 280
/// The stream is closed 281
/// </exception> 282
/// <exception cref="ZipException"> 283
/// The Zip stream ends early 284
/// </exception> 285
public void CloseEntry() 286
{ 287
if (crc == null) { 288
throw new InvalidOperationException("Closed."); 289
} 290
291
if (entry == null) { 292
return; 293
} 294
295
if (method == (int)CompressionMethod.Deflated) { 296
if ((flags & 8) != 0) { 297
// We don't know how much we must skip, read until end. 298
byte[] tmp = new byte[2048]; 299
while (Read(tmp, 0, tmp.Length) > 0) 300
; 301
// read will close this entry 302
return; 303
} 304
csize -= inf.TotalIn; 305
inputBuffer.Available -= inf.RemainingInput; 306
} 307
308
if (inputBuffer.Available > csize && csize >= 0) { 309
inputBuffer.Available = (int)((long)inputBuffer.Available - csize); 310
} else { 311
csize




