/* * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test * @bug 6334171 * @summary Test that zip file's data descriptor is written correctly.
*/
/** * Bug 6252735 ZipEntry contains wasteful "temporary storage" fields introduced * a regression. The value of the general purpose flag bit in a LOC header is * written incorrectly, because it is computed on stale data. * <p> * With the bug present, zipbytes2 is written incorrectly: when the LOC * header is written, (flag(e) & 8) == 8. This is correct: the data will be * compressed, so we don't have data length, etc. yet; that should be written * in the DataDescriptor after the data itself is written. However, when the * ZipOutputStream that wraps zipbytes2 is closed, the data length _is_ * available, therefore (flag(e) & 8) = 0), therefore the DataDescriptor is * not written. This is why, with the bug, zipbytes1.length == sizeof(ext * header) + zipbytes2.length. * <p> * The result is an invalid LOC header in zipbytes2. So when we again use * copyZip, we attempt to read a data length not from the LOC header but from * the non-existent EXT header, and at that position in the file is some * arbitrary and incorrect value.
*/ publicclass DataDescriptor { staticvoid copyZip(ZipInputStream in, ZipOutputStream out) throws IOException { byte[] buffer = newbyte[1 << 14]; for (ZipEntry ze; (ze = in.getNextEntry()) != null; ) {
out.putNextEntry(ze); // When the bug is present, it shows up here. The second call to // copyZip will throw an exception while reading data. for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
}
privatestaticvoid realMain(String[] args) throws Throwable { // Create zip output in byte array zipbytes
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry e = new ZipEntry("testdir/foo"); byte[] data = "entry data".getBytes("ASCII");
zos.putNextEntry(e);
zos.write(data);
zos.close(); byte[] zipbytes1 = baos.toByteArray(); int length1 = zipbytes1.length;
System.out.println("zip bytes pre-copy length=" + length1);
// Make a ZipInputStream around zipbytes, and use // copyZip to get a new byte array.
ZipInputStream zis = new ZipInputStream( new ByteArrayInputStream(zipbytes1));
baos.reset();
zos = new ZipOutputStream(baos);
copyZip(zis, zos);
zos.close(); byte[] zipbytes2 = baos.toByteArray(); int length2 = zipbytes2.length; // When the bug is present, pre- and post-copy lengths are different!
System.out.println("zip bytes post-copy length=" + length2);
// Now use copyZip again on the bytes resulting from the previous // copy. When the bug is present, copyZip will get an exception this // time.
baos.reset();
zos = new ZipOutputStream(baos);
copyZip(new ZipInputStream(new ByteArrayInputStream(zipbytes2)), zos);
zos.close(); byte[] zipbytes3 = baos.toByteArray(); int length3 = zipbytes3.length;
System.out.println("zip bytes post-copy length=" + length3);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.