public ZipEntry(ZipEntry e, String name) { this.name = name; known = e.known; size = e.size; compressedSize = e.compressedSize; crc = e.crc; dostime = e.dostime; time = e.time; method = e.method; extra = e.extra; comment = e.comment; }
/** * Creates an input stream reading the given zip entry as * uncompressed data. Normally zip entry should be an entry * returned by getEntry() or entries(). * * This implementation returns null if the requested entry does not * exist. This decision is not obviously correct, however, it does * appear to mirror Sun's implementation, and it is consistant with * their javadoc. On the other hand, the old JCL book, 2nd Edition, * claims that this should return a "non-null ZIP entry". We have * chosen for now ignore the old book, as modern versions of Ant (an * important application) depend on this behaviour. See discussion * in this thread: * http://gcc.gnu.org/ml/java-patches/2004-q2/msg00602.html * * @param entry the entry to create an InputStream for. * @return the input stream, or null if the requested entry does not exist. * * @exception IllegalStateException when the ZipFile has already been closed * @exception IOException if a i/o error occured. * @exception ZipException if the Zip archive is malformed. */ public Stream getInputStream(ZipEntry entry) { checkClosed(); Dictionary<String, ZipEntry> entries = getEntries(); String name = entry.getName(); ZipEntry zipEntry = entries[name]; if (zipEntry == null) return null; PartialInputStream inp = new PartialInputStream(raf, 1024); inp.seek(zipEntry.offset); if (inp.readLeInt() != LOCSIG) throw new System.Exception("Wrong Local header signature: " + name); inp.skip(4); if (zipEntry.getMethod() != inp.readLeShort()) throw new System.Exception("Compression method mismatch: " + name); inp.skip(16); int nameLen = inp.readLeShort(); int extraLen = inp.readLeShort(); inp.skip(nameLen + extraLen); inp.setLength(zipEntry.getCompressedSize()); int method = zipEntry.getMethod(); switch (method) { case ZipOutputStream.STORED: return inp; case ZipOutputStream.DEFLATED: inp.addDummyByte(); return new System.IO.Compression.DeflateStream(inp, System.IO.Compression.CompressionMode.Decompress); // Inflater inf = new Inflater(true); //int sz = (int)entry.getSize(); //return new InflateStream(sz,inp,inf); //return new InflaterInputStream(inp, inf) //{ // public int available() // { // if (sz == -1) // return super.available(); // if (super.available() != 0) // return sz - inf.getTotalOut(); // return 0; // } //}; default: throw new System.Exception("Unknown compression method " + method); } }
/** * Creates a copy of the given zip entry. * @param e the entry to copy. */ public ZipEntry(ZipEntry e) : this(e, e.name) { }
/** * Read the central directory of a zip file and fill the entries * array. This is called exactly once when first needed. It is called * while holding the lock on <code>raf</code>. * * @exception IOException if a i/o error occured. * @exception ZipException if the central directory is malformed */ private void readEntries() { /* Search for the End Of Central Directory. When a zip comment is * present the directory may start earlier. * Note that a comment has a maximum length of 64K, so that is the * maximum we search backwards. */ PartialInputStream inp = new PartialInputStream(raf, 4096); long pos = raf.Length - ENDHDR; long top = Math.Max(0, pos - 65536); do { if (pos < top) throw new System.Exception ("central directory not found, probably not a zip file: " + name); inp.seek(pos--); } while (inp.readLeInt() != ENDSIG); if (inp.skip(ENDTOT - ENDNRD) != ENDTOT - ENDNRD) throw new System.Exception(name); int count = inp.readLeShort(); if (inp.skip(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ) throw new System.Exception(name); int centralOffset = inp.readLeInt(); entries = new Dictionary<String, ZipEntry>(count + count / 2); inp.seek(centralOffset); for (int i = 0; i < count; i++) { if (inp.readLeInt() != CENSIG) throw new System.Exception("Wrong Central Directory signature: " + name); inp.skip(6); int method = inp.readLeShort(); int dostime = inp.readLeInt(); int crc = inp.readLeInt(); int csize = inp.readLeInt(); int size = inp.readLeInt(); int nameLen = inp.readLeShort(); int extraLen = inp.readLeShort(); int commentLen = inp.readLeShort(); inp.skip(8); int offset = inp.readLeInt(); String _name = inp.readString(nameLen); ZipEntry entry = new ZipEntry(_name); entry.setMethod(method); entry.setCrc(crc & 0xffffffffL); entry.setSize(size & 0xffffffffL); entry.setCompressedSize(csize & 0xffffffffL); entry.setDOSTime(dostime); if (extraLen > 0) { byte[] extra = new byte[extraLen]; inp.readFully(extra); entry.setExtra(extra); } if (commentLen > 0) { entry.setComment(inp.readString(commentLen)); } entry.offset = offset; entries[_name] = entry; } }