1032N/A/*
3909N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1032N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1032N/A *
1032N/A * This code is free software; you can redistribute it and/or modify it
1032N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1032N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1032N/A *
1032N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1032N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1032N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1032N/A * version 2 for more details (a copy is included in the LICENSE file that
1032N/A * accompanied this code).
1032N/A *
1032N/A * You should have received a copy of the GNU General Public License version
1032N/A * 2 along with this work; if not, write to the Free Software Foundation,
1032N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1032N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1032N/A */
1032N/A
1032N/Aimport java.io.*;
1032N/Aimport java.nio.*;
4365N/Aimport java.nio.file.*;
4365N/Aimport java.nio.file.attribute.*;
4365N/Aimport java.nio.file.spi.*;
1032N/Aimport java.util.*;
1032N/Aimport java.util.zip.*;
1032N/A
4365N/Aimport static java.nio.file.StandardCopyOption.*;
1032N/A
4365N/Apublic class LargeZip {
4365N/A // If true, don't delete large ZIP file created for test.
4365N/A static final boolean debug = System.getProperty("debug") != null;
1032N/A
4365N/A //static final int DATA_LEN = 1024 * 1024;
4365N/A static final int DATA_LEN = 80 * 1024;
4365N/A static final int DATA_SIZE = 8;
1032N/A
4365N/A static long fileSize = 6L * 1024L * 1024L * 1024L; // 6GB
4365N/A
4365N/A static boolean userFile = false;
4365N/A static byte[] data;
4365N/A static File largeFile;
4365N/A static String lastEntryName;
1032N/A
4365N/A /* args can be empty, in which case check a 3 GB file which is created for
4365N/A * this test (and then deleted). Or it can be a number, in which case
4365N/A * that designates the size of the file that's created for this test (and
4365N/A * then deleted). Or it can be the name of a file to use for the test, in
4365N/A * which case it is *not* deleted. Note that in this last case, the data
4365N/A * comparison might fail.
4365N/A */
4365N/A static void realMain (String[] args) throws Throwable {
4365N/A if (args.length > 0) {
4365N/A try {
4365N/A fileSize = Long.parseLong(args[0]);
4365N/A System.out.println("Testing with file of size " + fileSize);
4365N/A } catch (NumberFormatException ex) {
4365N/A largeFile = new File(args[0]);
4365N/A if (!largeFile.exists()) {
4365N/A throw new Exception("Specified file " + args[0] + " does not exist");
4365N/A }
4365N/A userFile = true;
4365N/A System.out.println("Testing with user-provided file " + largeFile);
4365N/A }
4365N/A }
4365N/A File testDir = null;
4365N/A if (largeFile == null) {
4365N/A testDir = new File(System.getProperty("test.scratch", "."),
4365N/A "LargeZip");
4365N/A if (testDir.exists()) {
4365N/A if (!testDir.delete()) {
4365N/A throw new Exception("Cannot delete already-existing test directory");
4365N/A }
4365N/A }
4365N/A check(!testDir.exists() && testDir.mkdirs());
4365N/A largeFile = new File(testDir, "largezip.zip");
4365N/A createLargeZip();
4365N/A } else {
4365N/A if (args.length > 1)
4365N/A updateLargeZip(args[1]); // add new entry with zfs
4365N/A }
4365N/A readLargeZip1();
4365N/A readLargeZip2();
1032N/A
4365N/A if (!userFile && !debug) {
4365N/A check(largeFile.delete());
4365N/A check(testDir.delete());
4365N/A }
4365N/A }
4365N/A
4365N/A static void createLargeZip() throws Throwable {
4365N/A int iterations = DATA_LEN / DATA_SIZE;
4365N/A ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE);
4365N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
4365N/A for (int i = 0; i < iterations; i++) {
4365N/A bb.putDouble(0, Math.random());
4365N/A baos.write(bb.array(), 0, DATA_SIZE);
4365N/A }
4365N/A data = baos.toByteArray();
4365N/A
4365N/A try (FileOutputStream fos = new FileOutputStream(largeFile);
4365N/A BufferedOutputStream bos = new BufferedOutputStream(fos);
4365N/A ZipOutputStream zos = new ZipOutputStream(bos))
4365N/A {
4365N/A long length = 0;
4365N/A while (length < fileSize) {
4365N/A ZipEntry ze = new ZipEntry("entry-" + length);
4365N/A lastEntryName = ze.getName();
4365N/A zos.putNextEntry(ze);
4365N/A zos.write(data, 0, data.length);
4365N/A zos.closeEntry();
4365N/A length = largeFile.length();
4365N/A }
4365N/A System.out.println("Last entry written is " + lastEntryName);
4365N/A }
4365N/A }
4365N/A
4365N/A private static byte buf[] = new byte[4096];
4365N/A
4365N/A static void checkEntry(ZipEntry e, InputStream is) throws Throwable {
4365N/A long N = 0;
4365N/A int n = 0;
4365N/A while ((n = is.read(buf)) >= 0) {
4365N/A N += n;
4365N/A }
4365N/A check(N == e.getSize());
4365N/A }
1032N/A
4365N/A static void readLargeZip1() throws Throwable {
4365N/A ZipFile zipFile = new ZipFile(largeFile);
4365N/A ZipEntry entry = null;
4365N/A String entryName = null;
4365N/A int count = 0;
4365N/A System.out.println("ZipFile:");
4365N/A Enumeration<? extends ZipEntry> entries = zipFile.entries();
4365N/A while (entries.hasMoreElements()) {
4365N/A entry = entries.nextElement();
4365N/A entryName = entry.getName();
4365N/A System.out.println(" checking " + entryName);
4365N/A if (!entry.isDirectory()) {
4365N/A try (InputStream zeis = zipFile.getInputStream(entry)) {
4365N/A checkEntry(entry, zeis);
4365N/A }
4365N/A }
4365N/A count++;
4365N/A }
4365N/A System.out.println("Number of entries read: " + count);
4365N/A check(!entry.isDirectory());
4365N/A if (userFile || check(entryName.equals(lastEntryName))) {
4365N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
4365N/A InputStream is = zipFile.getInputStream(entry);
4365N/A int len;
4365N/A while ((len = is.read(buf)) >= 0) {
4365N/A baos.write(buf, 0, len);
4365N/A }
4365N/A baos.close();
4365N/A is.close();
4365N/A if (!userFile)
4365N/A check(Arrays.equals(data, baos.toByteArray()));
4365N/A }
4365N/A }
1032N/A
4365N/A static void readLargeZip2() throws Throwable {
4365N/A System.out.println("ZipInputStream:");
4365N/A try (FileInputStream fis = new FileInputStream(largeFile);
4365N/A BufferedInputStream bis = new BufferedInputStream(fis);
4365N/A ZipInputStream zis = new ZipInputStream(bis))
4365N/A {
4365N/A ZipEntry entry = null;
4365N/A String entryName = null;
4365N/A int count = 0;
4365N/A while ((entry = zis.getNextEntry()) != null) {
4365N/A entryName = entry.getName();
1032N/A
4365N/A System.out.println(" checking " + entryName +
4365N/A ", method=" + entry.getMethod());
4365N/A if (entryName.equals(lastEntryName)) {
4365N/A break;
4365N/A }
4365N/A if (!entry.isDirectory()) {
4365N/A checkEntry(entry, zis);
4365N/A }
4365N/A count++;
4365N/A }
4365N/A System.out.println("Number of entries read: " + count);
4365N/A System.out.println("Last entry read is " + entryName);
4365N/A if (!userFile) {
4365N/A check(!entry.isDirectory());
4365N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
4365N/A byte buf[] = new byte[4096];
4365N/A int len;
4365N/A while ((len = zis.read(buf)) >= 0) {
4365N/A baos.write(buf, 0, len);
4365N/A }
4365N/A baos.close();
4365N/A check(Arrays.equals(data, baos.toByteArray()));
4365N/A check(zis.getNextEntry() == null);
4365N/A }
4365N/A }
4365N/A }
1032N/A
4365N/A private static void updateFile(FileSystem fs, Path src) throws IOException {
4365N/A Path dst = fs.getPath(src.toString());
4365N/A Path parent = dst.getParent();
4365N/A if (parent != null && Files.notExists(parent))
4365N/A Files.createDirectories(parent);
4365N/A Files.copy(src, dst, REPLACE_EXISTING);
4365N/A }
4365N/A
4365N/A private static FileSystemProvider getZipFSProvider() {
4365N/A for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
4365N/A if ("jar".equalsIgnoreCase(provider.getScheme()))
4365N/A return provider;
4365N/A }
4365N/A return null;
4365N/A }
4365N/A
4365N/A static void updateLargeZip(String pName) throws Throwable {
4365N/A FileSystemProvider provider = getZipFSProvider();
4365N/A if (provider == null) {
4365N/A System.err.println("ZIP filesystem provider is not installed");
4365N/A System.exit(1);
4365N/A }
4365N/A Map<String, Object> env = env = new HashMap<>();
4365N/A try (FileSystem fs = provider.newFileSystem(largeFile.toPath(), env)) {
4365N/A Path path = FileSystems.getDefault().getPath(pName);
4365N/A Files.walkFileTree(
4365N/A path,
4365N/A new SimpleFileVisitor<Path>() {
4365N/A @Override
4365N/A public FileVisitResult visitFile(Path file,
4365N/A BasicFileAttributes attrs)
4365N/A throws IOException
4365N/A {
4365N/A updateFile(fs, file);
4365N/A return FileVisitResult.CONTINUE;
4365N/A }
4365N/A });
4365N/A }
4365N/A }
1032N/A
1032N/A
4365N/A //--------------------- Infrastructure ---------------------------
4365N/A static volatile int passed = 0, failed = 0;
4365N/A static void pass() {passed++;}
4365N/A static void pass(String msg) {System.out.println(msg); passed++;}
4365N/A static void fail() {failed++; Thread.dumpStack();}
4365N/A static void fail(String msg) {System.out.println(msg); fail();}
4365N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
4365N/A static void unexpected(Throwable t, String msg) {
4365N/A System.out.println(msg); failed++; t.printStackTrace();}
4365N/A static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
4365N/A static void equal(Object x, Object y) {
4365N/A if (x == null ? y == null : x.equals(y)) pass();
4365N/A else fail(x + " not equal to " + y);}
4365N/A public static void main(String[] args) throws Throwable {
4365N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
4365N/A System.out.println("\nPassed = " + passed + " failed = " + failed);
4365N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1032N/A}