提交 58fa6e63 编写于 作者: S sherman

7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64...

7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
Summary: fixed the wrong size when writing out the cen table for ZIP64
Reviewed-by: alanb
上级 5e7ec2bc
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
package com.sun.nio.zipfs; package com.sun.nio.zipfs;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.EOFException; import java.io.EOFException;
...@@ -1165,7 +1166,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -1165,7 +1166,6 @@ public class ZipFileSystem extends FileSystem {
// sync the zip file system, if there is any udpate // sync the zip file system, if there is any udpate
private void sync() throws IOException { private void sync() throws IOException {
//System.out.printf("->sync(%s) starting....!%n", toString()); //System.out.printf("->sync(%s) starting....!%n", toString());
// check ex-closer // check ex-closer
if (!exChClosers.isEmpty()) { if (!exChClosers.isEmpty()) {
for (ExChannelCloser ecc : exChClosers) { for (ExChannelCloser ecc : exChClosers) {
...@@ -1179,84 +1179,84 @@ public class ZipFileSystem extends FileSystem { ...@@ -1179,84 +1179,84 @@ public class ZipFileSystem extends FileSystem {
if (!hasUpdate) if (!hasUpdate)
return; return;
Path tmpFile = createTempFileInSameDirectoryAs(zfpath); Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
OutputStream os = Files.newOutputStream(tmpFile, WRITE); try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmpFile, WRITE)))
ArrayList<Entry> elist = new ArrayList<>(inodes.size()); {
long written = 0; ArrayList<Entry> elist = new ArrayList<>(inodes.size());
byte[] buf = new byte[8192]; long written = 0;
Entry e = null; byte[] buf = new byte[8192];
Entry e = null;
// write loc
for (IndexNode inode : inodes.values()) { // write loc
if (inode instanceof Entry) { // an updated inode for (IndexNode inode : inodes.values()) {
e = (Entry)inode; if (inode instanceof Entry) { // an updated inode
try { e = (Entry)inode;
if (e.type == Entry.COPY) { try {
// entry copy: the only thing changed is the "name" if (e.type == Entry.COPY) {
// and "nlen" in LOC header, so we udpate/rewrite the // entry copy: the only thing changed is the "name"
// LOC in new file and simply copy the rest (data and // and "nlen" in LOC header, so we udpate/rewrite the
// ext) without enflating/deflating from the old zip // LOC in new file and simply copy the rest (data and
// file LOC entry. // ext) without enflating/deflating from the old zip
written += copyLOCEntry(e, true, os, written, buf); // file LOC entry.
} else { // NEW, FILECH or CEN written += copyLOCEntry(e, true, os, written, buf);
e.locoff = written; } else { // NEW, FILECH or CEN
written += e.writeLOC(os); // write loc header e.locoff = written;
if (e.bytes != null) { // in-memory, deflated written += e.writeLOC(os); // write loc header
os.write(e.bytes); // already if (e.bytes != null) { // in-memory, deflated
written += e.bytes.length; os.write(e.bytes); // already
} else if (e.file != null) { // tmp file written += e.bytes.length;
try (InputStream is = Files.newInputStream(e.file)) { } else if (e.file != null) { // tmp file
int n; try (InputStream is = Files.newInputStream(e.file)) {
if (e.type == Entry.NEW) { // deflated already int n;
while ((n = is.read(buf)) != -1) { if (e.type == Entry.NEW) { // deflated already
os.write(buf, 0, n);
written += n;
}
} else if (e.type == Entry.FILECH) {
// the data are not deflated, use ZEOS
try (OutputStream os2 = new EntryOutputStream(e, os)) {
while ((n = is.read(buf)) != -1) { while ((n = is.read(buf)) != -1) {
os2.write(buf, 0, n); os.write(buf, 0, n);
written += n;
} }
} else if (e.type == Entry.FILECH) {
// the data are not deflated, use ZEOS
try (OutputStream os2 = new EntryOutputStream(e, os)) {
while ((n = is.read(buf)) != -1) {
os2.write(buf, 0, n);
}
}
written += e.csize;
if ((e.flag & FLAG_DATADESCR) != 0)
written += e.writeEXT(os);
} }
written += e.csize;
if ((e.flag & FLAG_DATADESCR) != 0)
written += e.writeEXT(os);
} }
Files.delete(e.file);
tmppaths.remove(e.file);
} else {
// dir, 0-length data
} }
Files.delete(e.file);
tmppaths.remove(e.file);
} else {
// dir, 0-length data
} }
elist.add(e);
} catch (IOException x) {
x.printStackTrace(); // skip any in-accurate entry
}
} else { // unchanged inode
if (inode.pos == -1) {
continue; // pseudo directory node
}
e = Entry.readCEN(this, inode.pos);
try {
written += copyLOCEntry(e, false, os, written, buf);
elist.add(e);
} catch (IOException x) {
x.printStackTrace(); // skip any wrong entry
} }
elist.add(e);
} catch (IOException x) {
x.printStackTrace(); // skip any in-accurate entry
}
} else { // unchanged inode
if (inode.pos == -1) {
continue; // pseudo directory node
}
e = Entry.readCEN(this, inode.pos);
try {
written += copyLOCEntry(e, false, os, written, buf);
elist.add(e);
} catch (IOException x) {
x.printStackTrace(); // skip any wrong entry
} }
} }
}
// now write back the cen and end table // now write back the cen and end table
end.cenoff = written; end.cenoff = written;
for (Entry entry : elist) { for (Entry entry : elist) {
written += entry.writeCEN(os); written += entry.writeCEN(os);
}
end.centot = elist.size();
end.cenlen = written - end.cenoff;
end.write(os, written);
} }
end.centot = elist.size();
end.cenlen = written - end.cenoff;
end.write(os, written);
os.close();
if (!streams.isEmpty()) { if (!streams.isEmpty()) {
// //
// TBD: ExChannelCloser should not be necessary if we only // TBD: ExChannelCloser should not be necessary if we only
...@@ -1959,7 +1959,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -1959,7 +1959,7 @@ public class ZipFileSystem extends FileSystem {
writeBytes(os, name); writeBytes(os, name);
if (elen64 != 0) { if (elen64 != 0) {
writeShort(os, EXTID_ZIP64);// Zip64 extra writeShort(os, EXTID_ZIP64);// Zip64 extra
writeShort(os, elen64); // size of "this" extra block writeShort(os, elen64 - 4); // size of "this" extra block
if (size0 == ZIP64_MINVAL) if (size0 == ZIP64_MINVAL)
writeLong(os, size); writeLong(os, size);
if (csize0 == ZIP64_MINVAL) if (csize0 == ZIP64_MINVAL)
......
...@@ -25,173 +25,242 @@ ...@@ -25,173 +25,242 @@
import java.io.*; import java.io.*;
import java.nio.*; import java.nio.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.file.spi.*;
import java.util.*; import java.util.*;
import java.util.zip.*; import java.util.zip.*;
import static java.nio.file.StandardCopyOption.*;
public class LargeZip { public class LargeZip {
// If true, don't delete large ZIP file created for test. // If true, don't delete large ZIP file created for test.
static final boolean debug = System.getProperty("debug") != null; static final boolean debug = System.getProperty("debug") != null;
//static final int DATA_LEN = 1024 * 1024; //static final int DATA_LEN = 1024 * 1024;
static final int DATA_LEN = 80 * 1024; static final int DATA_LEN = 80 * 1024;
static final int DATA_SIZE = 8; static final int DATA_SIZE = 8;
static long fileSize = 6L * 1024L * 1024L * 1024L; // 6GB static long fileSize = 6L * 1024L * 1024L * 1024L; // 6GB
static boolean userFile = false; static boolean userFile = false;
static byte[] data;
static byte[] data; static File largeFile;
static File largeFile; static String lastEntryName;
static String lastEntryName;
/* args can be empty, in which case check a 3 GB file which is created for
/* args can be empty, in which case check a 3 GB file which is created for * this test (and then deleted). Or it can be a number, in which case
* this test (and then deleted). Or it can be a number, in which case * that designates the size of the file that's created for this test (and
* that designates the size of the file that's created for this test (and * then deleted). Or it can be the name of a file to use for the test, in
* then deleted). Or it can be the name of a file to use for the test, in * which case it is *not* deleted. Note that in this last case, the data
* which case it is *not* deleted. Note that in this last case, the data * comparison might fail.
* comparison might fail. */
*/ static void realMain (String[] args) throws Throwable {
static void realMain (String[] args) throws Throwable { if (args.length > 0) {
if (args.length > 0) { try {
try { fileSize = Long.parseLong(args[0]);
fileSize = Long.parseLong(args[0]); System.out.println("Testing with file of size " + fileSize);
System.out.println("Testing with file of size " + fileSize); } catch (NumberFormatException ex) {
} catch (NumberFormatException ex) { largeFile = new File(args[0]);
largeFile = new File(args[0]); if (!largeFile.exists()) {
if (!largeFile.exists()) { throw new Exception("Specified file " + args[0] + " does not exist");
throw new Exception("Specified file " + args[0] + " does not exist"); }
} userFile = true;
userFile = true; System.out.println("Testing with user-provided file " + largeFile);
System.out.println("Testing with user-provided file " + largeFile); }
} }
} File testDir = null;
File testDir = null; if (largeFile == null) {
if (largeFile == null) { testDir = new File(System.getProperty("test.scratch", "."),
testDir = new File(System.getProperty("test.scratch", "."), "LargeZip");
"LargeZip"); if (testDir.exists()) {
if (testDir.exists()) { if (!testDir.delete()) {
if (!testDir.delete()) { throw new Exception("Cannot delete already-existing test directory");
throw new Exception("Cannot delete already-existing test directory"); }
} }
} check(!testDir.exists() && testDir.mkdirs());
check(!testDir.exists() && testDir.mkdirs()); largeFile = new File(testDir, "largezip.zip");
largeFile = new File(testDir, "largezip.zip"); createLargeZip();
createLargeZip(); } else {
} if (args.length > 1)
updateLargeZip(args[1]); // add new entry with zfs
readLargeZip1(); }
readLargeZip2(); readLargeZip1();
readLargeZip2();
if (!userFile && !debug) {
check(largeFile.delete()); if (!userFile && !debug) {
check(testDir.delete()); check(largeFile.delete());
} check(testDir.delete());
} }
}
static void createLargeZip() throws Throwable {
int iterations = DATA_LEN / DATA_SIZE; static void createLargeZip() throws Throwable {
ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE); int iterations = DATA_LEN / DATA_SIZE;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE);
for (int i = 0; i < iterations; i++) { ByteArrayOutputStream baos = new ByteArrayOutputStream();
bb.putDouble(0, Math.random()); for (int i = 0; i < iterations; i++) {
baos.write(bb.array(), 0, DATA_SIZE); bb.putDouble(0, Math.random());
} baos.write(bb.array(), 0, DATA_SIZE);
data = baos.toByteArray(); }
data = baos.toByteArray();
try (FileOutputStream fos = new FileOutputStream(largeFile);
BufferedOutputStream bos = new BufferedOutputStream(fos); try (FileOutputStream fos = new FileOutputStream(largeFile);
ZipOutputStream zos = new ZipOutputStream(bos)) BufferedOutputStream bos = new BufferedOutputStream(fos);
{ ZipOutputStream zos = new ZipOutputStream(bos))
long length = 0; {
while (length < fileSize) { long length = 0;
ZipEntry ze = new ZipEntry("entry-" + length); while (length < fileSize) {
lastEntryName = ze.getName(); ZipEntry ze = new ZipEntry("entry-" + length);
zos.putNextEntry(ze); lastEntryName = ze.getName();
zos.write(data, 0, data.length); zos.putNextEntry(ze);
zos.closeEntry(); zos.write(data, 0, data.length);
length = largeFile.length(); zos.closeEntry();
} length = largeFile.length();
System.out.println("Last entry written is " + lastEntryName); }
} System.out.println("Last entry written is " + lastEntryName);
} }
}
static void readLargeZip1() throws Throwable {
ZipFile zipFile = new ZipFile(largeFile); private static byte buf[] = new byte[4096];
ZipEntry entry = null;
String entryName = null; static void checkEntry(ZipEntry e, InputStream is) throws Throwable {
int count = 0; long N = 0;
Enumeration<? extends ZipEntry> entries = zipFile.entries(); int n = 0;
while (entries.hasMoreElements()) { while ((n = is.read(buf)) >= 0) {
entry = entries.nextElement(); N += n;
entryName = entry.getName(); }
count++; check(N == e.getSize());
} }
System.out.println("Number of entries read: " + count);
System.out.println("Last entry read is " + entryName); static void readLargeZip1() throws Throwable {
check(!entry.isDirectory()); ZipFile zipFile = new ZipFile(largeFile);
if (check(entryName.equals(lastEntryName))) { ZipEntry entry = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); String entryName = null;
InputStream is = zipFile.getInputStream(entry); int count = 0;
byte buf[] = new byte[4096]; System.out.println("ZipFile:");
int len; Enumeration<? extends ZipEntry> entries = zipFile.entries();
while ((len = is.read(buf)) >= 0) { while (entries.hasMoreElements()) {
baos.write(buf, 0, len); entry = entries.nextElement();
} entryName = entry.getName();
baos.close(); System.out.println(" checking " + entryName);
is.close(); if (!entry.isDirectory()) {
check(Arrays.equals(data, baos.toByteArray())); try (InputStream zeis = zipFile.getInputStream(entry)) {
} checkEntry(entry, zeis);
} }
}
count++;
static void readLargeZip2() throws Throwable { }
try (FileInputStream fis = new FileInputStream(largeFile); System.out.println("Number of entries read: " + count);
BufferedInputStream bis = new BufferedInputStream(fis); check(!entry.isDirectory());
ZipInputStream zis = new ZipInputStream(bis)) if (userFile || check(entryName.equals(lastEntryName))) {
{ ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipEntry entry = null; InputStream is = zipFile.getInputStream(entry);
String entryName = null; int len;
int count = 0; while ((len = is.read(buf)) >= 0) {
while ((entry = zis.getNextEntry()) != null) { baos.write(buf, 0, len);
entryName = entry.getName(); }
if (entryName.equals(lastEntryName)) { baos.close();
break; is.close();
} if (!userFile)
count++; check(Arrays.equals(data, baos.toByteArray()));
} }
System.out.println("Number of entries read: " + count); }
System.out.println("Last entry read is " + entryName);
check(!entry.isDirectory()); static void readLargeZip2() throws Throwable {
System.out.println("ZipInputStream:");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (FileInputStream fis = new FileInputStream(largeFile);
BufferedInputStream bis = new BufferedInputStream(fis);
byte buf[] = new byte[4096]; ZipInputStream zis = new ZipInputStream(bis))
int len; {
while ((len = zis.read(buf)) >= 0) { ZipEntry entry = null;
baos.write(buf, 0, len); String entryName = null;
} int count = 0;
baos.close(); while ((entry = zis.getNextEntry()) != null) {
check(Arrays.equals(data, baos.toByteArray())); entryName = entry.getName();
check(zis.getNextEntry() == null);
} System.out.println(" checking " + entryName +
} ", method=" + entry.getMethod());
if (entryName.equals(lastEntryName)) {
break;
//--------------------- Infrastructure --------------------------- }
static volatile int passed = 0, failed = 0; if (!entry.isDirectory()) {
static void pass() {passed++;} checkEntry(entry, zis);
static void pass(String msg) {System.out.println(msg); passed++;} }
static void fail() {failed++; Thread.dumpStack();} count++;
static void fail(String msg) {System.out.println(msg); fail();} }
static void unexpected(Throwable t) {failed++; t.printStackTrace();} System.out.println("Number of entries read: " + count);
static void unexpected(Throwable t, String msg) { System.out.println("Last entry read is " + entryName);
System.out.println(msg); failed++; t.printStackTrace();} if (!userFile) {
static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;} check(!entry.isDirectory());
static void equal(Object x, Object y) { ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (x == null ? y == null : x.equals(y)) pass(); byte buf[] = new byte[4096];
else fail(x + " not equal to " + y);} int len;
public static void main(String[] args) throws Throwable { while ((len = zis.read(buf)) >= 0) {
try {realMain(args);} catch (Throwable t) {unexpected(t);} baos.write(buf, 0, len);
System.out.println("\nPassed = " + passed + " failed = " + failed); }
if (failed > 0) throw new AssertionError("Some tests failed");} baos.close();
check(Arrays.equals(data, baos.toByteArray()));
check(zis.getNextEntry() == null);
}
}
}
private static void updateFile(FileSystem fs, Path src) throws IOException {
Path dst = fs.getPath(src.toString());
Path parent = dst.getParent();
if (parent != null && Files.notExists(parent))
Files.createDirectories(parent);
Files.copy(src, dst, REPLACE_EXISTING);
}
private static FileSystemProvider getZipFSProvider() {
for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
if ("jar".equalsIgnoreCase(provider.getScheme()))
return provider;
}
return null;
}
static void updateLargeZip(String pName) throws Throwable {
FileSystemProvider provider = getZipFSProvider();
if (provider == null) {
System.err.println("ZIP filesystem provider is not installed");
System.exit(1);
}
Map<String, Object> env = env = new HashMap<>();
try (FileSystem fs = provider.newFileSystem(largeFile.toPath(), env)) {
Path path = FileSystems.getDefault().getPath(pName);
Files.walkFileTree(
path,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs)
throws IOException
{
updateFile(fs, file);
return FileVisitResult.CONTINUE;
}
});
}
}
//--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0;
static void pass() {passed++;}
static void pass(String msg) {System.out.println(msg); passed++;}
static void fail() {failed++; Thread.dumpStack();}
static void fail(String msg) {System.out.println(msg); fail();}
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
static void unexpected(Throwable t, String msg) {
System.out.println(msg); failed++; t.printStackTrace();}
static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
static void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.println("\nPassed = " + passed + " failed = " + failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册