< prev index next >

src/java.base/share/classes/java/util/zip/ZipFile.java

Print this page

        

*** 323,332 **** --- 323,339 ---- * @throws IOException if an I/O error has occurred * @throws IllegalStateException if the zip file has been closed */ public InputStream getInputStream(ZipEntry entry) throws IOException { Objects.requireNonNull(entry, "entry"); + ZipCryption zipCryption = null; + + if (entry.isPassphraseRequired()) { + zipCryption = entry.zipCryption; + Objects.requireNonNull(zipCryption, "Passphrase is required."); + } + int pos = -1; ZipFileInputStream in = null; synchronized (this) { ensureOpen(); if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
*** 335,347 **** pos = zsrc.getEntryPos(zc.getBytes(entry.name), false); } if (pos == -1) { return null; } ! in = new ZipFileInputStream(zsrc.cen, pos); switch (CENHOW(zsrc.cen, pos)) { case STORED: synchronized (streams) { streams.put(in, null); } return in; case DEFLATED: --- 342,364 ---- pos = zsrc.getEntryPos(zc.getBytes(entry.name), false); } if (pos == -1) { return null; } ! in = new ZipFileInputStream(zsrc.cen, pos, zipCryption); switch (CENHOW(zsrc.cen, pos)) { case STORED: + if (entry.isPassphraseRequired()) { + entry.encryptionHeader = + new byte[TraditionalZipCryption.ENCRYPTION_HEADER_SIZE]; + in.readRaw(entry.encryptionHeader, 0, + entry.encryptionHeader.length); + if (!entry.isValidPassphrase()) { + throw new ZipException("possibly incorrect passphrase"); + } + } + synchronized (streams) { streams.put(in, null); } return in; case DEFLATED:
*** 353,362 **** --- 370,390 ---- } if (size <= 0) { size = 4096; } Inflater inf = getInflater(); + + if (entry.isPassphraseRequired()) { + entry.encryptionHeader = + new byte[TraditionalZipCryption.ENCRYPTION_HEADER_SIZE]; + in.readRaw(entry.encryptionHeader, 0, + entry.encryptionHeader.length); + if (!entry.isValidPassphrase()) { + throw new ZipException("possibly incorrect passphrase"); + } + } + InputStream is = new ZipFileInflaterInputStream(in, inf, (int)size); synchronized (streams) { streams.put(is, inf); } return is;
*** 656,677 **** private class ZipFileInputStream extends InputStream { private volatile boolean closeRequested; private long pos; // current position within entry data protected long rem; // number of remaining bytes within entry protected long size; // uncompressed size of this entry ! ZipFileInputStream(byte[] cen, int cenpos) throws IOException { rem = CENSIZ(cen, cenpos); size = CENLEN(cen, cenpos); pos = CENOFF(cen, cenpos); // zip64 if (rem == ZIP64_MAGICVAL || size == ZIP64_MAGICVAL || pos == ZIP64_MAGICVAL) { checkZIP64(cen, cenpos); } // negative for lazy initialization, see getDataOffset(); pos = - (pos + ZipFile.this.zsrc.locpos); } private void checkZIP64(byte[] cen, int cenpos) throws IOException { int off = cenpos + CENHDR + CENNAM(cen, cenpos); int end = off + CENEXT(cen, cenpos); --- 684,708 ---- private class ZipFileInputStream extends InputStream { private volatile boolean closeRequested; private long pos; // current position within entry data protected long rem; // number of remaining bytes within entry protected long size; // uncompressed size of this entry + private ZipCryption zipCryption; // ZIP encrypt/decrypt engine ! ZipFileInputStream(byte[] cen, int cenpos, ZipCryption zipCryption) ! throws IOException { rem = CENSIZ(cen, cenpos); size = CENLEN(cen, cenpos); pos = CENOFF(cen, cenpos); // zip64 if (rem == ZIP64_MAGICVAL || size == ZIP64_MAGICVAL || pos == ZIP64_MAGICVAL) { checkZIP64(cen, cenpos); } // negative for lazy initialization, see getDataOffset(); pos = - (pos + ZipFile.this.zsrc.locpos); + this.zipCryption = zipCryption; } private void checkZIP64(byte[] cen, int cenpos) throws IOException { int off = cenpos + CENHDR + CENNAM(cen, cenpos); int end = off + CENEXT(cen, cenpos);
*** 729,738 **** --- 760,779 ---- } return pos; } public int read(byte b[], int off, int len) throws IOException { + len = readRaw(b, off, len); + + if (zipCryption != null) { + zipCryption.decryptBytes(b, off, len); + } + + return len; + } + + public int readRaw(byte b[], int off, int len) throws IOException { synchronized (ZipFile.this) { ensureOpenOrZipException(); initDataOffset(); if (rem == 0) { return -1;
*** 1178,1189 **** zerror("invalid CEN header (bad signature)"); int method = CENHOW(cen, pos); int nlen = CENNAM(cen, pos); int elen = CENEXT(cen, pos); int clen = CENCOM(cen, pos); - if ((CENFLG(cen, pos) & 1) != 0) - zerror("invalid CEN header (encrypted entry)"); if (method != STORED && method != DEFLATED) zerror("invalid CEN header (bad compression method: " + method + ")"); if (pos + CENHDR + nlen > limit) zerror("invalid CEN header (bad header size)"); // Record the CEN offset and the name hash in our hash cell. --- 1219,1228 ----
< prev index next >