ZipFSTester.java revision 3569
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * This code is free software; you can redistribute it and/or modify it
c869993e79c1eafbec61a56bf6cea848fe754c71xy * under the terms of the GNU General Public License version 2 only, as
c869993e79c1eafbec61a56bf6cea848fe754c71xy * published by the Free Software Foundation.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * This code is distributed in the hope that it will be useful, but WITHOUT
c869993e79c1eafbec61a56bf6cea848fe754c71xy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c869993e79c1eafbec61a56bf6cea848fe754c71xy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c869993e79c1eafbec61a56bf6cea848fe754c71xy * version 2 for more details (a copy is included in the LICENSE file that
c869993e79c1eafbec61a56bf6cea848fe754c71xy * accompanied this code).
c869993e79c1eafbec61a56bf6cea848fe754c71xy * You should have received a copy of the GNU General Public License version
c869993e79c1eafbec61a56bf6cea848fe754c71xy * 2 along with this work; if not, write to the Free Software Foundation,
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c869993e79c1eafbec61a56bf6cea848fe754c71xy * or visit www.oracle.com if you need additional information or have any
c869993e79c1eafbec61a56bf6cea848fe754c71xy * questions.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Tests various zipfs operations.
c869993e79c1eafbec61a56bf6cea848fe754c71xypublic class ZipFSTester {
c869993e79c1eafbec61a56bf6cea848fe754c71xy public static void main(String[] args) throws Throwable {
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem fs = newZipFileSystem(Paths.get(args[0]),
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("path existence check failed!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("parent existence check failed!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy // clone a fs and test on it
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem copy = newZipFileSystem(tmpfsPath, env)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>())) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy // newFileSystem(path...) should not throw exception
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem fsPath = provider.newFileSystem(tmpfsPath, new HashMap<String, Object>())){}
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("newFileSystem(uri...) does not throw exception");
c869993e79c1eafbec61a56bf6cea848fe754c71xy // prepare a src
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl provider.newFileSystem(new File(System.getProperty("test.src", ".")).toPath(),
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl throw new RuntimeException("newFileSystem() opens a directory as zipfs");
c869993e79c1eafbec61a56bf6cea848fe754c71xy provider.newFileSystem(src, new HashMap<String, Object>());
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("newFileSystem() opens a non-zip file as zipfs");
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path dst2 = getPathWithParents(fs, "/xyz" + rdm.nextInt(100) +
c869993e79c1eafbec61a56bf6cea848fe754c71xy //dst.moveTo(dst2);
c869993e79c1eafbec61a56bf6cea848fe754c71xy // moveout
c869993e79c1eafbec61a56bf6cea848fe754c71xy // newInputStream on dir
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (IOException x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy // newFileChannel() copy in, out and verify via fch
c869993e79c1eafbec61a56bf6cea848fe754c71xy // test channels
c869993e79c1eafbec61a56bf6cea848fe754c71xy } finally {
c869993e79c1eafbec61a56bf6cea848fe754c71xy // create a new filesystem, copy everything from fs
c869993e79c1eafbec61a56bf6cea848fe754c71xy public void run() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (Exception x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy public void run() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (Exception x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy public void run() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (Exception x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy public void run() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (Exception x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (IOException x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy //x.printStackTrace();
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static FileSystem newZipFileSystem(Path path, Map<String, ?> env)
c869993e79c1eafbec61a56bf6cea848fe754c71xy new URI("jar", path.toUri().toString(), null), env, null);
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void list(Path path, List<String> files, List<String> dirs )
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void z2zcopy(FileSystem src, FileSystem dst, String path,
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy path + (path.endsWith("/")?"":"/") + child.getFileName(),
c869993e79c1eafbec61a56bf6cea848fe754c71xy switch (method) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy //fchCopy(srcPath, dstPath);
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void z2zmove(FileSystem src, FileSystem dst, String path)
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy path + (path.endsWith("/")?"":"/") + child.getFileName());
c869993e79c1eafbec61a56bf6cea848fe754c71xy //System.out.println("moving..." + path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy private void indent() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy int n = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (n++ < indent)
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("%s%n", file.getFileName().toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void mkdirs(Path path) throws IOException {
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void rmdirs(Path path) throws IOException {
c869993e79c1eafbec61a56bf6cea848fe754c71xy // check the content of two paths are equal
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void checkEqual(Path src, Path dst) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy //System.out.printf("checking <%s> vs <%s>...%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy // src.toString(), dst.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (n == -1) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy // channels
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (SeekableByteChannel chSrc = Files.newByteChannel(src);
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (IOException x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void fchCopy(Path src, Path dst) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void chCopy(Path src, Path dst) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
c869993e79c1eafbec61a56bf6cea848fe754c71xy SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void streamCopy(Path src, Path dst) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy int n = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy int n = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy int N = 120;
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf(" sbc[0]: pos=%d, size=%d%n", sbc.position(), sbc.size());
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl // sbc.position(pos) is not supported in current version
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl // try the FileChannel
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (SeekableByteChannel sbc = fs.provider().newFileChannel(path, read)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy // create parents if does not exist