2 // Copyright (C) 2001 Mike Krueger
\r
4 // This file was translated from java, it was part of the GNU Classpath
\r
5 // Copyright (C) 2001 Free Software Foundation, Inc.
\r
7 // This program is free software; you can redistribute it and/or
\r
8 // modify it under the terms of the GNU General Public License
\r
9 // as published by the Free Software Foundation; either version 2
\r
10 // of the License, or (at your option) any later version.
\r
12 // This program is distributed in the hope that it will be useful,
\r
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 // GNU General Public License for more details.
\r
17 // You should have received a copy of the GNU General Public License
\r
18 // along with this program; if not, write to the Free Software
\r
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
21 // Linking this library statically or dynamically with other modules is
\r
22 // making a combined work based on this library. Thus, the terms and
\r
23 // conditions of the GNU General Public License cover the whole
\r
26 // As a special exception, the copyright holders of this library give you
\r
27 // permission to link this library with independent modules to produce an
\r
28 // executable, regardless of the license terms of these independent
\r
29 // modules, and to copy and distribute the resulting executable under
\r
30 // terms of your choice, provided that you also meet, for each linked
\r
31 // independent module, the terms and conditions of the license of that
\r
32 // module. An independent module is a module which is not derived from
\r
33 // or based on this library. If you modify this library, you may extend
\r
34 // this exception to your version of the library, but you are not
\r
35 // obligated to do so. If you do not wish to do so, delete this
\r
36 // exception statement from your version.
\r
42 using ICSharpCode.SharpZipLib.Checksums;
\r
43 using ICSharpCode.SharpZipLib.Zip.Compression;
\r
44 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
\r
46 namespace ICSharpCode.SharpZipLib.Zip
\r
49 /// This is a FilterInputStream that reads the files baseInputStream an zip archive
\r
50 /// one after another. It has a special method to get the zip entry of
\r
51 /// the next file. The zip entry contains information about the file name
\r
52 /// size, compressed size, CRC, etc.
\r
53 /// It includes support for STORED and DEFLATED entries.
\r
55 /// author of the original java version : Jochen Hoenicke
\r
57 /// <example> This sample shows how to read a zip file
\r
58 /// <code lang="C#">
\r
60 /// using System.Text;
\r
61 /// using System.IO;
\r
63 /// using NZlib.Zip;
\r
67 /// public static void Main(string[] args)
\r
69 /// ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
\r
71 /// ZipEntry theEntry;
\r
72 /// while ((theEntry = s.GetNextEntry()) != null) {
\r
73 /// int size = 2048;
\r
74 /// byte[] data = new byte[2048];
\r
76 /// Console.Write("Show contents (y/n) ?");
\r
77 /// if (Console.ReadLine() == "y") {
\r
79 /// size = s.Read(data, 0, data.Length);
\r
81 /// Console.Write(new ASCIIEncoding().GetString(data, 0, size));
\r
93 public class ZipInputStream : InflaterInputStream
\r
95 Crc32 crc = new Crc32();
\r
96 ZipEntry entry = null;
\r
102 string password = null;
\r
104 public string Password {
\r
114 /// Creates a new Zip input stream, reading a zip archive.
\r
116 public ZipInputStream(Stream baseInputStream) : base(baseInputStream, new Inflater(true))
\r
122 avail = len = baseInputStream.Read(buf, 0, buf.Length);
\r
125 int ReadBuf(byte[] outBuf, int offset, int length)
\r
133 if (length > avail) {
\r
134 length = (int)avail;
\r
136 System.Array.Copy(buf, len - (int)avail, outBuf, offset, length);
\r
141 void ReadFully(byte[] outBuf)
\r
144 int len = outBuf.Length;
\r
146 int count = ReadBuf(outBuf, off, len);
\r
148 throw new Exception();
\r
160 throw new ZipException("EOF in header");
\r
163 return buf[len - avail--] & 0xff;
\r
167 /// Read an unsigned short baseInputStream little endian byte order.
\r
171 return ReadLeByte() | (ReadLeByte() << 8);
\r
175 /// Read an int baseInputStream little endian byte order.
\r
179 return ReadLeShort() | (ReadLeShort() << 16);
\r
183 /// Read an int baseInputStream little endian byte order.
\r
187 return ReadLeInt() | (ReadLeInt() << 32);
\r
191 /// Open the next entry from the zip archive, and return its description.
\r
192 /// If the previous entry wasn't closed, this method will close it.
\r
194 public ZipEntry GetNextEntry()
\r
197 throw new InvalidOperationException("Closed.");
\r
199 if (entry != null) {
\r
203 if (this.cryptbuffer != null) {
\r
204 if (avail == 0 && inf.RemainingInput != 0) {
\r
205 avail = inf.RemainingInput - 16;
\r
208 baseInputStream.Position -= this.len;
\r
209 baseInputStream.Read(this.buf, 0, this.len);
\r
212 int header = ReadLeInt();
\r
214 // -jr- added end sig for empty zip files, Zip64 end sig and digital sig for files that have them...
\r
215 if (header == ZipConstants.CENSIG ||
\r
216 header == ZipConstants.ENDSIG ||
\r
217 header == ZipConstants.CENDIGITALSIG ||
\r
218 header == ZipConstants.CENSIG64) {
\r
219 // Central Header reached or end of empty zip file
\r
223 // -jr- 07-Dec-2003 ignore spanning temporary signatures if found
\r
224 // SPANNINGSIG is same as descriptor signature and is untested as yet.
\r
225 if (header == ZipConstants.SPANTEMPSIG || header == ZipConstants.SPANNINGSIG) {
\r
226 header = ReadLeInt();
\r
229 if (header != ZipConstants.LOCSIG) {
\r
230 throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
\r
233 short version = (short)ReadLeShort();
\r
235 flags = ReadLeShort();
\r
236 method = ReadLeShort();
\r
237 uint dostime = (uint)ReadLeInt();
\r
238 int crc2 = ReadLeInt();
\r
239 csize = ReadLeInt();
\r
240 size = ReadLeInt();
\r
241 int nameLen = ReadLeShort();
\r
242 int extraLen = ReadLeShort();
\r
243 bool isCrypted = (flags & 1) == 1;
\r
244 if (method == ZipOutputStream.STORED && (!isCrypted && csize != size || (isCrypted && csize - 12 != size))) {
\r
245 throw new ZipException("Stored, but compressed != uncompressed");
\r
248 byte[] buffer = new byte[nameLen];
\r
251 string name = ZipConstants.ConvertToString(buffer);
\r
253 entry = new ZipEntry(name);
\r
254 entry.IsCrypted = isCrypted;
\r
255 entry.Version = (ushort)version;
\r
256 if (method != 0 && method != 8) {
\r
257 throw new ZipException("unknown compression method " + method);
\r
259 entry.CompressionMethod = (CompressionMethod)method;
\r
261 if ((flags & 8) == 0) {
\r
262 entry.Crc = crc2 & 0xFFFFFFFFL;
\r
263 entry.Size = size & 0xFFFFFFFFL;
\r
264 entry.CompressedSize = csize & 0xFFFFFFFFL;
\r
267 entry.DosTime = dostime;
\r
269 if (extraLen > 0) {
\r
270 byte[] extra = new byte[extraLen];
\r
272 entry.ExtraData = extra;
\r
275 // test for encryption
\r
277 if (password == null) {
\r
278 throw new ZipException("No password set.");
\r
280 InitializePassword(password);
\r
281 cryptbuffer = new byte[12];
\r
282 ReadFully(cryptbuffer);
\r
283 DecryptBlock(cryptbuffer, 0, cryptbuffer.Length);
\r
284 if ((flags & 8) == 0) {// -jr- 10-Feb-2004 Dont yet know correct size here....
\r
288 cryptbuffer = null;
\r
291 if (method == ZipOutputStream.DEFLATED && avail > 0) {
\r
292 System.Array.Copy(buf, len - (int)avail, buf, 0, (int)avail);
\r
296 DecryptBlock(buf, 0, Math.Min((int)csize, len));
\r
298 inf.SetInput(buf, 0, len);
\r
303 private void ReadDataDescr()
\r
305 if (ReadLeInt() != ZipConstants.EXTSIG) {
\r
306 throw new ZipException("Data descriptor signature not found");
\r
308 entry.Crc = ReadLeInt() & 0xFFFFFFFFL;
\r
309 csize = ReadLeInt();
\r
310 size = ReadLeInt();
\r
311 entry.Size = size & 0xFFFFFFFFL;
\r
312 entry.CompressedSize = csize & 0xFFFFFFFFL;
\r
316 /// Closes the current zip entry and moves to the next one.
\r
318 public void CloseEntry()
\r
321 throw new InvalidOperationException("Closed.");
\r
324 if (entry == null) {
\r
328 if (method == ZipOutputStream.DEFLATED) {
\r
329 if ((flags & 8) != 0) {
\r
330 /* We don't know how much we must skip, read until end. */
\r
331 byte[] tmp = new byte[2048];
\r
332 while (Read(tmp, 0, tmp.Length) > 0)
\r
334 /* read will close this entry */
\r
337 csize -= inf.TotalIn;
\r
338 avail = inf.RemainingInput;
\r
340 if (avail > csize && csize >= 0) {
\r
345 while (csize != 0) {
\r
346 int skipped = (int)base.Skip(csize & 0xFFFFFFFFL);
\r
348 if (skipped <= 0) {
\r
349 throw new ZipException("zip archive ends early.");
\r
358 if (method == ZipOutputStream.DEFLATED) {
\r
364 public override int Available {
\r
366 return entry != null ? 1 : 0;
\r
371 /// Reads a byte from the current zip entry.
\r
374 /// the byte or -1 on EOF.
\r
376 /// <exception name="System.IO.IOException">
\r
377 /// IOException if a i/o error occured.
\r
379 /// <exception name="ICSharpCode.SharpZipLib.ZipException">
\r
380 /// ZipException if the deflated stream is corrupted.
\r
382 public override int ReadByte()
\r
384 byte[] b = new byte[1];
\r
385 if (Read(b, 0, 1) <= 0) {
\r
388 return b[0] & 0xff;
\r
392 /// Reads a block of bytes from the current zip entry.
\r
395 /// the number of bytes read (may be smaller, even before EOF), or -1 on EOF.
\r
397 /// <exception name="Exception">
\r
398 /// IOException if a i/o error occured.
\r
399 /// ZipException if the deflated stream is corrupted.
\r
401 public override int Read(byte[] b, int off, int len)
\r
404 throw new InvalidOperationException("Closed.");
\r
407 if (entry == null) {
\r
410 bool finished = false;
\r
413 case ZipOutputStream.DEFLATED:
\r
414 len = base.Read(b, off, len);
\r
415 if (len <= 0) { // TODO BUG1 -jr- Check this was < 0 but avail was not adjusted causing failure in later calls
\r
416 if (!inf.IsFinished) {
\r
417 throw new ZipException("Inflater not finished!?");
\r
419 avail = inf.RemainingInput;
\r
421 // BUG1 -jr- With bit 3 set you dont yet know the size
\r
422 if ((flags & 8) == 0 && (inf.TotalIn != csize || inf.TotalOut != size)) {
\r
423 throw new ZipException("size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
\r
430 case ZipOutputStream.STORED:
\r
431 if (len > csize && csize >= 0) {
\r
434 len = ReadBuf(b, off, len);
\r
444 throw new ZipException("EOF in stored block");
\r
448 // decrypting crypted data
\r
449 if (cryptbuffer != null) {
\r
450 DecryptBlock(b, off, len);
\r
457 crc.Update(b, off, len);
\r
461 if ((flags & 8) != 0) {
\r
465 if ((crc.Value & 0xFFFFFFFFL) != entry.Crc && entry.Crc != -1) {
\r
466 throw new ZipException("CRC mismatch");
\r
475 /// Closes the zip file.
\r
477 /// <exception name="Exception">
\r
478 /// if a i/o error occured.
\r
480 public override void Close()
\r