/* args can be empty, in which case check a 3 GB file which is created for *thistest(andthendeleted).Oritcanbeanumber,inwhichcase *thatdesignatesthesizeofthefilethat'screatedforthistest(and *thendeleted).Oritcanbethenameofafiletouseforthetest,in *whichcaseitis*not*deleted.Notethatinthislastcase,thedata *comparisonmightfail.
*/ staticvoid realMain (String[] args) throws Throwable { if (args.length > 0) { try {
fileSize = Long.parseLong(args[0]);
System.out.println("Testing with file of size " + fileSize);
} catch (NumberFormatException ex) {
largeFile = new File(args[0]); if (!largeFile.exists()) { thrownew Exception("Specified file " + args[0] + " does not exist");
}
userFile = true;
System.out.println("Testing with user-provided file " + largeFile);
}
}
File testDir = null; if (largeFile == null) {
testDir = new File(System.getProperty("test.scratch", "."), "LargeZip"); if (testDir.exists()) { if (!testDir.delete()) { thrownew Exception("Cannot delete already-existing test directory");
}
}
check(!testDir.exists() && testDir.mkdirs());
largeFile = new File(testDir, "largezip.zip");
createLargeZip();
} else { if (args.length > 1)
updateLargeZip(args[1]); // add new entry with zfs
}
readLargeZip1();
readLargeZip2();
if (!userFile && !debug) {
check(largeFile.delete());
check(testDir.delete());
}
}
staticvoid createLargeZip() throws Throwable { int iterations = DATA_LEN / DATA_SIZE;
ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (int i = 0; i < iterations; i++) {
bb.putDouble(0, Math.random());
baos.write(bb.array(), 0, DATA_SIZE);
}
data = baos.toByteArray();
try (FileOutputStream fos = new FileOutputStream(largeFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos))
{ long length = 0; while (length < fileSize) {
ZipEntry ze = new ZipEntry("entry-" + length);
lastEntryName = ze.getName();
zos.putNextEntry(ze);
zos.write(data, 0, data.length);
zos.closeEntry();
length = largeFile.length();
}
System.out.println("Last entry written is " + lastEntryName);
}
}
privatestaticbyte buf[] = newbyte[4096];
staticvoid checkEntry(ZipEntry e, InputStream is) throws Throwable { long N = 0; int n = 0; while ((n = is.read(buf)) >= 0) {
N += n;
}
check(N == e.getSize());
}
staticvoid readLargeZip1() throws Throwable {
ZipFile zipFile = new ZipFile(largeFile);
ZipEntry entry = null;
String entryName = null; int count = 0;
System.out.println("ZipFile:");
Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) {
entry = entries.nextElement();
entryName = entry.getName();
System.out.println(" checking " + entryName); if (!entry.isDirectory()) { try (InputStream zeis = zipFile.getInputStream(entry)) {
checkEntry(entry, zeis);
}
}
count++;
}
System.out.println("Number of entries read: " + count);
check(!entry.isDirectory()); if (userFile || check(entryName.equals(lastEntryName))) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = zipFile.getInputStream(entry); int len; while ((len = is.read(buf)) >= 0) {
baos.write(buf, 0, len);
}
baos.close();
is.close(); if (!userFile)
check(Arrays.equals(data, baos.toByteArray()));
}
}
staticvoid readLargeZip2() throws Throwable {
System.out.println("ZipInputStream:"); try (FileInputStream fis = new FileInputStream(largeFile);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zis = new ZipInputStream(bis))
{
ZipEntry entry = null;
String entryName = null; int count = 0; while ((entry = zis.getNextEntry()) != null) {
entryName = entry.getName();
System.out.println(" checking " + entryName + ", method=" + entry.getMethod()); if (entryName.equals(lastEntryName)) { break;
} if (!entry.isDirectory()) {
checkEntry(entry, zis);
}
count++;
}
System.out.println("Number of entries read: " + count);
System.out.println("Last entry read is " + entryName); if (!userFile) {
check(!entry.isDirectory());
ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf[] = newbyte[4096]; int len; while ((len = zis.read(buf)) >= 0) {
baos.write(buf, 0, len);
}
baos.close();
check(Arrays.equals(data, baos.toByteArray()));
check(zis.getNextEntry() == null);
}
}
}
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 und die Messung sind noch experimentell.