ZipFSTester.java revision 3478
0N/A/*
553N/A * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation.
0N/A *
553N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
553N/A */
553N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.nio.file.*;
0N/Aimport java.nio.file.spi.*;
0N/Aimport java.nio.file.attribute.*;
49N/Aimport java.net.*;
49N/Aimport java.util.*;
0N/Aimport java.util.zip.*;
0N/A
0N/Aimport static java.nio.file.StandardOpenOption.*;
0N/Aimport static java.nio.file.StandardCopyOption.*;
0N/A
0N/A/*
0N/A * Tests various zipfs operations.
0N/A */
0N/A
580N/Apublic class ZipFSTester {
580N/A
0N/A public static void main(String[] args) throws Throwable {
0N/A
0N/A try (FileSystem fs = newZipFileSystem(Paths.get(args[0]),
0N/A new HashMap<String, Object>()))
0N/A {
0N/A test0(fs);
0N/A test1(fs);
0N/A test2(fs); // more tests
0N/A }
0N/A }
0N/A
0N/A static void test0(FileSystem fs)
0N/A throws Exception
0N/A {
0N/A List<String> list = new LinkedList<>();
0N/A try (ZipFile zf = new ZipFile(fs.toString())) {
0N/A Enumeration<? extends ZipEntry> zes = zf.entries();
0N/A while (zes.hasMoreElements()) {
0N/A list.add(zes.nextElement().getName());
0N/A }
0N/A for (String pname : list) {
0N/A Path path = fs.getPath(pname);
112N/A if (!Files.exists(path))
0N/A throw new RuntimeException("path existence check failed!");
0N/A while ((path = path.getParent()) != null) {
0N/A if (!Files.exists(path))
0N/A throw new RuntimeException("parent existence check failed!");
0N/A }
0N/A }
0N/A }
112N/A }
0N/A
0N/A static void test1(FileSystem fs)
0N/A throws Exception
0N/A {
0N/A Random rdm = new Random();
0N/A // clone a fs and test on it
0N/A Path tmpfsPath = getTempPath();
0N/A Map<String, Object> env = new HashMap<String, Object>();
0N/A env.put("create", "true");
0N/A FileSystem fs0 = newZipFileSystem(tmpfsPath, env);
0N/A z2zcopy(fs, fs0, "/", 0);
0N/A fs0.close(); // sync to file
0N/A
0N/A try (fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>())) {
0N/A
0N/A FileSystemProvider provider = fs.provider();
0N/A // newFileSystem(path...) should not throw exception
0N/A try (FileSystem fsPath = provider.newFileSystem(tmpfsPath, new HashMap<String, Object>())){}
0N/A try (FileSystem fsUri = provider.newFileSystem(
0N/A new URI("jar", tmpfsPath.toUri().toString(), null),
0N/A new HashMap<String, Object>()))
0N/A {
0N/A throw new RuntimeException("newFileSystem(uri...) does not throw exception");
0N/A } catch (FileSystemAlreadyExistsException fsaee) {}
0N/A
0N/A // prepare a src
0N/A Path src = getTempPath();
0N/A String tmpName = src.toString();
0N/A OutputStream os = Files.newOutputStream(src);
408N/A byte[] bits = new byte[12345];
408N/A rdm.nextBytes(bits);
408N/A os.write(bits);
408N/A os.close();
408N/A
408N/A // copyin
408N/A Path dst = getPathWithParents(fs, tmpName);
408N/A Files.copy(src, dst);
408N/A checkEqual(src, dst);
408N/A
408N/A // copy
408N/A Path dst2 = getPathWithParents(fs, "/xyz" + rdm.nextInt(100) +
0N/A "/efg" + rdm.nextInt(100) + "/foo.class");
0N/A Files.copy(dst, dst2);
0N/A //dst.moveTo(dst2);
0N/A checkEqual(src, dst2);
0N/A
0N/A // delete
0N/A Files.delete(dst);
0N/A if (Files.exists(dst))
0N/A throw new RuntimeException("Failed!");
0N/A
0N/A // moveout
0N/A Path dst3 = Paths.get(tmpName + "_Tmp");
0N/A Files.move(dst2, dst3);
0N/A checkEqual(src, dst3);
0N/A
0N/A // delete
0N/A if (Files.exists(dst2))
0N/A throw new RuntimeException("Failed!");
0N/A Files.delete(dst3);
0N/A if (Files.exists(dst3))
0N/A throw new RuntimeException("Failed!");
0N/A
0N/A // newInputStream on dir
0N/A Path parent = dst2.getParent();
0N/A try {
0N/A Files.newInputStream(parent);
0N/A throw new RuntimeException("Failed");
0N/A } catch (FileSystemException e) {
0N/A e.printStackTrace(); // expected fse
0N/A }
0N/A
0N/A // rmdirs
0N/A try {
0N/A rmdirs(parent);
0N/A } catch (IOException x) {
0N/A x.printStackTrace();
0N/A }
0N/A
0N/A // newFileChannel() copy in, out and verify via fch
0N/A fchCopy(src, dst); // in
0N/A checkEqual(src, dst);
0N/A Path tmp = Paths.get(tmpName + "_Tmp");
0N/A fchCopy(dst, tmp); // out
0N/A checkEqual(src, tmp);
0N/A Files.delete(tmp);
0N/A
0N/A // test channels
0N/A channel(fs, dst);
0N/A Files.delete(dst);
0N/A Files.delete(src);
0N/A } finally {
0N/A if (Files.exists(tmpfsPath))
0N/A Files.delete(tmpfsPath);
0N/A }
0N/A }
112N/A
0N/A static void test2(FileSystem fs) throws Exception {
0N/A
0N/A Path fs1Path = getTempPath();
0N/A Path fs2Path = getTempPath();
0N/A Path fs3Path = getTempPath();
0N/A
408N/A // create a new filesystem, copy everything from fs
408N/A Map<String, Object> env = new HashMap<String, Object>();
408N/A env.put("create", "true");
408N/A FileSystem fs0 = newZipFileSystem(fs1Path, env);
408N/A
408N/A final FileSystem fs2 = newZipFileSystem(fs2Path, env);
408N/A final FileSystem fs3 = newZipFileSystem(fs3Path, env);
0N/A
0N/A System.out.println("copy src: fs -> fs0...");
0N/A z2zcopy(fs, fs0, "/", 0); // copy fs -> fs1
0N/A fs0.close(); // dump to file
0N/A
0N/A System.out.println("open fs0 as fs1");
0N/A env = new HashMap<String, Object>();
0N/A final FileSystem fs1 = newZipFileSystem(fs1Path, env);
0N/A
0N/A System.out.println("listing...");
0N/A final ArrayList<String> files = new ArrayList<>();
0N/A final ArrayList<String> dirs = new ArrayList<>();
0N/A list(fs1.getPath("/"), files, dirs);
0N/A
0N/A Thread t0 = new Thread(new Runnable() {
0N/A public void run() {
0N/A List<String> list = new ArrayList<>(dirs);
0N/A Collections.shuffle(list);
0N/A for (String path : list) {
0N/A try {
0N/A z2zcopy(fs1, fs2, path, 0);
0N/A } catch (Exception x) {
0N/A x.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
0N/A });
0N/A
0N/A Thread t1 = new Thread(new Runnable() {
0N/A public void run() {
0N/A List<String> list = new ArrayList<>(dirs);
0N/A Collections.shuffle(list);
0N/A for (String path : list) {
0N/A try {
0N/A z2zcopy(fs1, fs2, path, 1);
0N/A } catch (Exception x) {
0N/A x.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
0N/A });
0N/A
0N/A Thread t2 = new Thread(new Runnable() {
0N/A public void run() {
0N/A List<String> list = new ArrayList<>(dirs);
0N/A Collections.shuffle(list);
0N/A for (String path : list) {
0N/A try {
0N/A z2zcopy(fs1, fs2, path, 2);
0N/A } catch (Exception x) {
0N/A x.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
0N/A });
0N/A
0N/A Thread t3 = new Thread(new Runnable() {
0N/A public void run() {
0N/A List<String> list = new ArrayList<>(files);
0N/A Collections.shuffle(list);
0N/A while (!list.isEmpty()) {
0N/A Iterator<String> itr = list.iterator();
0N/A while (itr.hasNext()) {
0N/A String path = itr.next();
0N/A try {
0N/A if (Files.exists(fs2.getPath(path))) {
0N/A z2zmove(fs2, fs3, path);
0N/A itr.remove();
0N/A }
0N/A } catch (FileAlreadyExistsException x){
0N/A itr.remove();
0N/A } catch (Exception x) {
0N/A x.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A });
0N/A
0N/A System.out.println("copying/removing...");
0N/A t0.start(); t1.start(); t2.start(); t3.start();
0N/A t0.join(); t1.join(); t2.join(); t3.join();
0N/A
0N/A System.out.println("closing: fs1, fs2");
0N/A fs1.close();
0N/A fs2.close();
0N/A
0N/A int failed = 0;
0N/A System.out.println("checkEqual: fs vs fs3");
0N/A for (String path : files) {
0N/A try {
0N/A checkEqual(fs.getPath(path), fs3.getPath(path));
0N/A } catch (IOException x) {
0N/A //x.printStackTrace();
0N/A failed++;
0N/A }
0N/A }
0N/A System.out.println("closing: fs3");
0N/A fs3.close();
0N/A
0N/A System.out.println("opening: fs3 as fs4");
0N/A FileSystem fs4 = newZipFileSystem(fs3Path, env);
0N/A
0N/A
0N/A ArrayList<String> files2 = new ArrayList<>();
0N/A ArrayList<String> dirs2 = new ArrayList<>();
0N/A list(fs4.getPath("/"), files2, dirs2);
0N/A
0N/A System.out.println("checkEqual: fs vs fs4");
0N/A for (String path : files2) {
0N/A checkEqual(fs.getPath(path), fs4.getPath(path));
0N/A }
0N/A System.out.println("walking: fs4");
0N/A walk(fs4.getPath("/"));
0N/A System.out.println("closing: fs4");
0N/A fs4.close();
0N/A System.out.printf("failed=%d%n", failed);
0N/A
0N/A Files.delete(fs1Path);
0N/A Files.delete(fs2Path);
0N/A Files.delete(fs3Path);
0N/A }
0N/A
0N/A private static FileSystem newZipFileSystem(Path path, Map<String, ?> env)
0N/A throws Exception
0N/A {
0N/A return FileSystems.newFileSystem(
0N/A new URI("jar", path.toUri().toString(), null), env, null);
0N/A }
0N/A
0N/A private static Path getTempPath() throws IOException
0N/A {
0N/A File tmp = File.createTempFile("testzipfs_", "zip");
0N/A tmp.delete(); // we need a clean path, no file
0N/A return tmp.toPath();
0N/A }
0N/A
0N/A private static void list(Path path, List<String> files, List<String> dirs )
0N/A throws IOException
0N/A {
0N/A if (Files.isDirectory(path)) {
0N/A try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
0N/A for (Path child : ds)
0N/A list(child, files, dirs);
0N/A }
0N/A dirs.add(path.toString());
0N/A } else {
266N/A files.add(path.toString());
0N/A }
0N/A }
0N/A
0N/A private static void z2zcopy(FileSystem src, FileSystem dst, String path,
0N/A int method)
0N/A throws IOException
0N/A {
0N/A Path srcPath = src.getPath(path);
0N/A Path dstPath = dst.getPath(path);
0N/A
0N/A if (Files.isDirectory(srcPath)) {
0N/A if (!Files.exists(dstPath)) {
0N/A try {
0N/A mkdirs(dstPath);
0N/A } catch (FileAlreadyExistsException x) {}
0N/A }
0N/A try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
0N/A for (Path child : ds) {
0N/A z2zcopy(src, dst,
0N/A path + (path.endsWith("/")?"":"/") + child.getFileName(),
0N/A method);
0N/A }
0N/A }
0N/A } else {
0N/A try {
0N/A if (Files.exists(dstPath))
0N/A return;
0N/A switch (method) {
0N/A case 0:
0N/A Files.copy(srcPath, dstPath);
0N/A break;
0N/A case 1:
0N/A chCopy(srcPath, dstPath);
0N/A break;
0N/A case 2:
0N/A //fchCopy(srcPath, dstPath);
0N/A streamCopy(srcPath, dstPath);
0N/A break;
0N/A }
266N/A } catch (FileAlreadyExistsException x) {}
266N/A }
266N/A }
266N/A
266N/A private static void z2zmove(FileSystem src, FileSystem dst, String path)
266N/A throws IOException
266N/A {
266N/A Path srcPath = src.getPath(path);
266N/A Path dstPath = dst.getPath(path);
266N/A
266N/A if (Files.isDirectory(srcPath)) {
266N/A if (!Files.exists(dstPath))
0N/A mkdirs(dstPath);
0N/A try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
0N/A for (Path child : ds) {
0N/A z2zmove(src, dst,
0N/A path + (path.endsWith("/")?"":"/") + child.getFileName());
0N/A }
0N/A }
0N/A } else {
266N/A //System.out.println("moving..." + path);
266N/A Path parent = dstPath.getParent();
266N/A if (parent != null && Files.notExists(parent))
266N/A mkdirs(parent);
266N/A Files.move(srcPath, dstPath);
266N/A }
266N/A }
266N/A
266N/A private static void walk(Path path) throws IOException
266N/A {
266N/A Files.walkFileTree(
266N/A path,
266N/A new SimpleFileVisitor<Path>() {
266N/A private int indent = 0;
266N/A private void indent() {
266N/A int n = 0;
266N/A while (n++ < indent)
266N/A System.out.printf(" ");
0N/A }
408N/A
408N/A @Override
408N/A public FileVisitResult visitFile(Path file,
408N/A BasicFileAttributes attrs)
408N/A {
408N/A indent();
408N/A System.out.printf("%s%n", file.getFileName().toString());
408N/A return FileVisitResult.CONTINUE;
596N/A }
408N/A
408N/A @Override
408N/A public FileVisitResult preVisitDirectory(Path dir,
408N/A BasicFileAttributes attrs)
408N/A {
408N/A indent();
408N/A System.out.printf("[%s]%n", dir.toString());
408N/A indent += 2;
408N/A return FileVisitResult.CONTINUE;
408N/A }
408N/A
0N/A @Override
0N/A public FileVisitResult postVisitDirectory(Path dir,
0N/A IOException ioe)
0N/A throws IOException
0N/A {
0N/A indent -= 2;
408N/A return FileVisitResult.CONTINUE;
0N/A }
0N/A });
0N/A }
0N/A
408N/A private static void mkdirs(Path path) throws IOException {
0N/A if (Files.exists(path))
408N/A return;
0N/A path = path.toAbsolutePath();
408N/A Path parent = path.getParent();
0N/A if (parent != null) {
0N/A if (Files.notExists(parent))
0N/A mkdirs(parent);
0N/A }
0N/A Files.createDirectory(path);
0N/A }
0N/A
0N/A private static void rmdirs(Path path) throws IOException {
0N/A while (path != null && path.getNameCount() != 0) {
0N/A Files.delete(path);
0N/A path = path.getParent();
0N/A }
0N/A }
0N/A
0N/A // check the content of two paths are equal
0N/A private static void checkEqual(Path src, Path dst) throws IOException
0N/A {
0N/A //System.out.printf("checking <%s> vs <%s>...%n",
0N/A // src.toString(), dst.toString());
0N/A
0N/A //streams
0N/A byte[] bufSrc = new byte[8192];
0N/A byte[] bufDst = new byte[8192];
0N/A try (InputStream isSrc = Files.newInputStream(src);
0N/A InputStream isDst = Files.newInputStream(dst))
408N/A {
408N/A int nSrc = 0;
408N/A while ((nSrc = isSrc.read(bufSrc)) != -1) {
0N/A int nDst = 0;
0N/A while (nDst < nSrc) {
0N/A int n = isDst.read(bufDst, nDst, nSrc - nDst);
0N/A if (n == -1) {
0N/A System.out.printf("checking <%s> vs <%s>...%n",
408N/A src.toString(), dst.toString());
0N/A throw new RuntimeException("CHECK FAILED!");
0N/A }
0N/A nDst += n;
0N/A }
408N/A while (--nSrc >= 0) {
0N/A if (bufSrc[nSrc] != bufDst[nSrc]) {
408N/A System.out.printf("checking <%s> vs <%s>...%n",
0N/A src.toString(), dst.toString());
0N/A throw new RuntimeException("CHECK FAILED!");
0N/A }
0N/A nSrc--;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // channels
0N/A try (SeekableByteChannel chSrc = Files.newByteChannel(src);
0N/A SeekableByteChannel chDst = Files.newByteChannel(dst))
0N/A {
0N/A if (chSrc.size() != chDst.size()) {
0N/A System.out.printf("src[%s].size=%d, dst[%s].size=%d%n",
0N/A chSrc.toString(), chSrc.size(),
0N/A chDst.toString(), chDst.size());
0N/A throw new RuntimeException("CHECK FAILED!");
0N/A }
0N/A ByteBuffer bbSrc = ByteBuffer.allocate(8192);
0N/A ByteBuffer bbDst = ByteBuffer.allocate(8192);
0N/A
0N/A int nSrc = 0;
0N/A while ((nSrc = chSrc.read(bbSrc)) != -1) {
0N/A int nDst = chDst.read(bbDst);
0N/A if (nSrc != nDst) {
0N/A System.out.printf("checking <%s> vs <%s>...%n",
0N/A src.toString(), dst.toString());
0N/A throw new RuntimeException("CHECK FAILED!");
0N/A }
0N/A while (--nSrc >= 0) {
0N/A if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
0N/A System.out.printf("checking <%s> vs <%s>...%n",
408N/A src.toString(), dst.toString());
408N/A throw new RuntimeException("CHECK FAILED!");
0N/A }
408N/A nSrc--;
0N/A }
0N/A bbSrc.flip();
0N/A bbDst.flip();
0N/A }
0N/A } catch (IOException x) {
0N/A x.printStackTrace();
0N/A }
408N/A }
408N/A
408N/A private static void fchCopy(Path src, Path dst) throws IOException
408N/A {
408N/A Set<OpenOption> read = new HashSet<>();
408N/A read.add(READ);
408N/A Set<OpenOption> openwrite = new HashSet<>();
408N/A openwrite.add(CREATE_NEW);
0N/A openwrite.add(WRITE);
408N/A
0N/A try (FileChannel srcFc = src.getFileSystem()
0N/A .provider()
0N/A .newFileChannel(src, read);
0N/A FileChannel dstFc = dst.getFileSystem()
408N/A .provider()
0N/A .newFileChannel(dst, openwrite))
408N/A {
0N/A ByteBuffer bb = ByteBuffer.allocate(8192);
408N/A while (srcFc.read(bb) >= 0) {
0N/A bb.flip();
0N/A dstFc.write(bb);
0N/A bb.clear();
0N/A }
0N/A }
408N/A }
0N/A
0N/A private static void chCopy(Path src, Path dst) throws IOException
0N/A {
408N/A Set<OpenOption> read = new HashSet<>();
0N/A read.add(READ);
0N/A Set<OpenOption> openwrite = new HashSet<>();
0N/A openwrite.add(CREATE_NEW);
0N/A openwrite.add(WRITE);
0N/A
0N/A try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
0N/A SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
0N/A {
0N/A
0N/A ByteBuffer bb = ByteBuffer.allocate(8192);
0N/A while (srcCh.read(bb) >= 0) {
0N/A bb.flip();
0N/A dstCh.write(bb);
0N/A bb.clear();
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void streamCopy(Path src, Path dst) throws IOException
0N/A {
0N/A byte[] buf = new byte[8192];
0N/A try (InputStream isSrc = Files.newInputStream(src);
0N/A OutputStream osDst = Files.newOutputStream(dst))
0N/A {
0N/A int n = 0;
0N/A while ((n = isSrc.read(buf)) != -1) {
0N/A osDst.write(buf, 0, n);
0N/A }
0N/A }
0N/A }
0N/A
0N/A static void channel(FileSystem fs, Path path)
0N/A throws Exception
0N/A {
0N/A System.out.println("test ByteChannel...");
0N/A Set<OpenOption> read = new HashSet<>();
0N/A read.add(READ);
0N/A int n = 0;
0N/A ByteBuffer bb = null;
0N/A ByteBuffer bb2 = null;
0N/A int N = 120;
0N/A
0N/A try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
0N/A System.out.printf(" sbc[0]: pos=%d, size=%d%n", sbc.position(), sbc.size());
0N/A bb = ByteBuffer.allocate((int)sbc.size());
0N/A n = sbc.read(bb);
0N/A System.out.printf(" sbc[1]: read=%d, pos=%d, size=%d%n",
0N/A n, sbc.position(), sbc.size());
0N/A bb2 = ByteBuffer.allocate((int)sbc.size());
0N/A }
0N/A
0N/A // sbc.position(pos) is not supported in current version
0N/A // try the FileChannel
0N/A try (SeekableByteChannel sbc = fs.provider().newFileChannel(path, read)) {
0N/A sbc.position(N);
0N/A System.out.printf(" sbc[2]: pos=%d, size=%d%n",
0N/A sbc.position(), sbc.size());
0N/A bb2.limit(100);
0N/A n = sbc.read(bb2);
0N/A System.out.printf(" sbc[3]: read=%d, pos=%d, size=%d%n",
0N/A n, sbc.position(), sbc.size());
0N/A System.out.printf(" sbc[4]: bb[%d]=%d, bb1[0]=%d%n",
0N/A N, bb.get(N) & 0xff, bb2.get(0) & 0xff);
0N/A }
0N/A }
0N/A
0N/A // create parents if does not exist
0N/A static Path getPathWithParents(FileSystem fs, String name)
0N/A throws Exception
0N/A {
0N/A Path path = fs.getPath(name);
0N/A Path parent = path.getParent();
0N/A if (parent != null && Files.notExists(parent))
0N/A mkdirs(parent);
0N/A return path;
0N/A }
0N/A}
0N/A