Demo.java revision 3216
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste/*
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste *
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * Redistribution and use in source and binary forms, with or without
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * modification, are permitted provided that the following conditions
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * are met:
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste *
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * - Redistributions of source code must retain the above copyright
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * notice, this list of conditions and the following disclaimer.
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste *
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * - Redistributions in binary form must reproduce the above copyright
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * notice, this list of conditions and the following disclaimer in the
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * documentation and/or other materials provided with the distribution.
ccf9d4a5c6453fa9f8b839baeee25147865fbb7dJames Phillpotts *
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * - Neither the name of Oracle nor the names of its
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * contributors may be used to endorse or promote products derived
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * from this software without specific prior written permission.
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts *
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts */
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.io.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.nio.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.nio.channels.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.nio.file.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.nio.file.attribute.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.net.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.text.DateFormat;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.text.SimpleDateFormat;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.util.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport static java.nio.file.StandardOpenOption.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport static java.nio.file.StandardCopyOption.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste/*
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * ZipFileSystem usage demo
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts *
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * java Demo action ZipfileName [...]
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts *
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * @author Xueming Shen
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts */
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottspublic class Demo {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts static enum Action {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts rename, // <java Demo rename zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // rename entry src to dst inside zipfile
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts movein, // <java Demo movein zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // move an external src file into zipfile
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // as entry dst
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste moveout, // <java Demo moveout zipfile src dst>
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // move a zipfile entry src out to dst
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste copy, // <java Demo copy zipfile src dst>
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste // copy entry src to dst inside zipfile
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts copyin, // <java Demo copyin zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // copy an external src file into zipfile
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // as entry dst
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts copyin_attrs, // <java Demo copyin_attrs zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // copy an external src file into zipfile
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // as entry dst, with attributes (timestamp)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts copyout, // <java Demo copyout zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // copy zipfile entry src" out to file dst
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts copyout_attrs, // <java Demo copyout_attrs zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts zzmove, // <java Demo zzmove zfsrc zfdst path>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // move entry path/dir from zfsrc to zfdst
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste zzcopy, // <java Demo zzcopy zfsrc zfdst path>
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // copy path from zipfile zfsrc to zipfile
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // zfdst
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste attrs, // <java Demo attrs zipfile path>
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste // printout the attributes of entry path
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste attrsspace, // <java Demo attrsspace zipfile path>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // printout the storespace attrs of entry path
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste setmtime, // <java Demo setmtime zipfile "MM/dd/yy-HH:mm:ss" path...>
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // set the lastModifiedTime of entry path
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts setatime, // <java Demo setatime zipfile "MM/dd/yy-HH:mm:ss" path...>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts setctime, // <java Demo setctime zipfile "MM/dd/yy-HH:mm:ss" path...>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts lsdir, // <java Demo lsdir zipfile dir>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // list dir's direct child files/dirs
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste mkdir, // <java Demo mkdir zipfile dir>
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste mkdirs, // <java Demo mkdirs zipfile dir>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts rmdirs, // <java Demo rmdirs zipfile dir>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts list, // <java Demo list zipfile [dir]>
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste // recursively list all entries of dir
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // via DirectoryStream
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts tlist, // <java Demo tlist zipfile [dir]>
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // list with buildDirTree=true
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts vlist, // <java Demo vlist zipfile [dir]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // recursively verbose list all entries of
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // dir via DirectoryStream
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts walk, // <java Demo walk zipfile [dir]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // recursively walk all entries of dir
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // via Files.walkFileTree
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts twalk, // <java Demo twalk zipfile [dir]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // walk with buildDirTree=true
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts extract, // <java Demo extract zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts update, // <java Demo extract zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts delete, // <java Demo delete zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts add, // <java Demo add zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts create, // <java Demo create zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // create a new zipfile if it doesn't exit
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // and then add the file(s) into it.
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts attrs2, // <java Demo attrs2 zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // test different ways to print attrs
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts prof,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public static void main(String[] args) throws Throwable {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Action action = Action.valueOf(args[0]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Map<String, Object> env = env = new HashMap<>();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (action == Action.create)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts env.put("create", "true");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (action == Action.tlist || action == Action.twalk)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts env.put("buildDirTree", true);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts FileSystem fs = FileSystems.newFileSystem(Paths.get(args[1]), env, null);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste try {
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste FileSystem fs2;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste Path path, src, dst;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts boolean isRename = false;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts switch (action) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case rename:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src = fs.getPath(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = fs.getPath(args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.moveTo(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case moveout:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src = fs.getPath(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = Paths.get(args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.moveTo(dst);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste break;
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste case movein:
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste src = Paths.get(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = fs.getPath(args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.moveTo(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case copy:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src = fs.getPath(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = fs.getPath(args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.copyTo(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case copyout:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src = fs.getPath(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = Paths.get(args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.copyTo(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste case copyin:
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste src = Paths.get(args[2]);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste dst = fs.getPath(args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.copyTo(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case copyin_attrs:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src = Paths.get(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = fs.getPath(args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.copyTo(dst, COPY_ATTRIBUTES);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case copyout_attrs:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src = fs.getPath(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = Paths.get(args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.copyTo(dst, COPY_ATTRIBUTES);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case zzmove:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs2 = FileSystems.newFileSystem(Paths.get(args[2]), env, null);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //sf1.getPath(args[3]).moveTo(fs2.getPath(args[3]));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts z2zmove(fs, fs2, args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs2.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case zzcopy:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs2 = FileSystems.newFileSystem(Paths.get(args[2]), env, null);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //sf1.getPath(args[3]).copyTo(fs2.getPath(args[3]));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts z2zcopy(fs, fs2, args[3]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs2.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case attrs:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (int i = 2; i < args.length; i++) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path = fs.getPath(args[i]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println(path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Attributes.readBasicFileAttributes(path).toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste break;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste case setmtime:
74b502cf428d057e78572e9fd66fb2f1861887b0Jaco Jooste DateFormat df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
63ee0c9f26b85deae17137a3965738153e0b2d20Gabor Melkvi Date newDatetime = df.parse(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (int i = 3; i < args.length; i++) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path = fs.getPath(args[i]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path.setAttribute("lastModifiedTime",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts FileTime.fromMillis(newDatetime.getTime()));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Attributes.readBasicFileAttributes(path).toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case setctime:
74b502cf428d057e78572e9fd66fb2f1861887b0Jaco Jooste df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
74b502cf428d057e78572e9fd66fb2f1861887b0Jaco Jooste newDatetime = df.parse(args[2]);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste for (int i = 3; i < args.length; i++) {
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste path = fs.getPath(args[i]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path.setAttribute("creationTime",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts FileTime.fromMillis(newDatetime.getTime()));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Attributes.readBasicFileAttributes(path).toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case setatime:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste newDatetime = df.parse(args[2]);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste for (int i = 3; i < args.length; i++) {
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste path = fs.getPath(args[i]);
63ee0c9f26b85deae17137a3965738153e0b2d20Gabor Melkvi path.setAttribute("lastAccessTime",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts FileTime.fromMillis(newDatetime.getTime()));
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste System.out.println(
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste Attributes.readBasicFileAttributes(path).toString());
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste case attrsspace:
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste path = fs.getPath("/");
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste FileStore fstore = path.getFileStore();
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste //System.out.println(fstore.getFileStoreAttributeView(FileStoreSpaceAttributeView.class)
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // .readAttributes());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // or
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste System.out.printf("filestore[%s]%n", fstore.name());
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste System.out.printf(" totalSpace: %d%n",
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste (Long)fstore.getAttribute("space:totalSpace"));
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste System.out.printf(" usableSpace: %d%n",
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste (Long)fstore.getAttribute("space:usableSpace"));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf(" unallocSpace: %d%n",
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste (Long)fstore.getAttribute("space:unallocatedSpace"));
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste break;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste case list:
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste case tlist:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (args.length < 3)
ccf9d4a5c6453fa9f8b839baeee25147865fbb7dJames Phillpotts list(fs.getPath("/"), false);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste else
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste list(fs.getPath(args[2]), false);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case vlist:
74b502cf428d057e78572e9fd66fb2f1861887b0Jaco Jooste if (args.length < 3)
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste list(fs.getPath("/"), true);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste else
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste list(fs.getPath(args[2]), true);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste break;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste case twalk:
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste case walk:
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste walk(fs.getPath((args.length > 2)? args[2] : "/"));
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste break;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste case extract:
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste if (args.length == 2) {
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste extract(fs, "/");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } else {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (int i = 2; i < args.length; i++) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts extract(fs, args[i]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case delete:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (int i = 2; i < args.length; i++)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs.getPath(args[i]).delete();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case create:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case add:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case update:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (int i = 2; i < args.length; i++) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts update(fs, args[i]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case lsdir:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path = fs.getPath(args[2]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts final String fStr = (args.length > 3)?args[3]:"";
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = path.newDirectoryStream(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts new DirectoryStream.Filter<Path>() {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public boolean accept(Path path) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return path.toString().contains(fStr);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts });
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (Path p : ds)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println(p);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case mkdir:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs.getPath(args[2]).createDirectory();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case mkdirs:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts mkdirs(fs.getPath(args[2]));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case attrs2:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (int i = 2; i < args.length; i++) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path = fs.getPath(args[i]);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("%n%s%n", path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println("-------(1)---------");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Attributes.readBasicFileAttributes(path).toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println("-------(2)---------");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Map<String, ?> map = path.readAttributes("zip:*");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (Map.Entry<String, ?> e : map.entrySet()) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf(" %s : %s%n", e.getKey(), e.getValue());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println("-------(3)---------");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts map = path.readAttributes("size,lastModifiedTime,isDirectory");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (Map.Entry<String, ?> e : map.entrySet()) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf(" %s : %s%n", e.getKey(), e.getValue());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case prof:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts list(fs.getPath("/"), false);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while (true) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Thread.sleep(10000);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //list(fs.getPath("/"), true);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println("sleeping...");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } catch (Exception x) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts x.printStackTrace();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } finally {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (fs != null)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static byte[] getBytes(String name) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return name.getBytes();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static String getString(byte[] name) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return new String(name);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void walk(Path path) throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Files.walkFileTree(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts new SimpleFileVisitor<Path>() {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private int indent = 0;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private void indent() {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts int n = 0;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while (n++ < indent)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf(" ");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts @Override
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult visitFile(Path file,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts BasicFileAttributes attrs)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts indent();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("%s%n", file.getName().toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return FileVisitResult.CONTINUE;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts @Override
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult preVisitDirectory(Path dir,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts BasicFileAttributes attrs)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts indent();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("[%s]%n", dir.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts indent += 2;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return FileVisitResult.CONTINUE;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts @Override
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult postVisitDirectory(Path dir,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts IOException ioe)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts indent -= 2;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return FileVisitResult.CONTINUE;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts });
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void update(FileSystem fs, String path) throws Throwable{
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path src = FileSystems.getDefault().getPath(path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (Boolean.TRUE.equals(src.getAttribute("isDirectory"))) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = src.newDirectoryStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (Path child : ds)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts update(fs, child.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ds.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } else {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path dst = fs.getPath(path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path parent = dst.getParent();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (parent != null && parent.notExists())
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts mkdirs(parent);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.copyTo(dst, REPLACE_EXISTING);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void extract(FileSystem fs, String path) throws Throwable{
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path src = fs.getPath(path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (Boolean.TRUE.equals(src.getAttribute("isDirectory"))) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = src.newDirectoryStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (Path child : ds)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts extract(fs, child.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ds.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } else {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (path.startsWith("/"))
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path = path.substring(1);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path dst = FileSystems.getDefault().getPath(path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path parent = dst.getParent();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (parent.notExists())
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts mkdirs(parent);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.copyTo(dst, REPLACE_EXISTING);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // use DirectoryStream
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void z2zcopy(FileSystem src, FileSystem dst, String path)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path srcPath = src.getPath(path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path dstPath = dst.getPath(path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (Boolean.TRUE.equals(srcPath.getAttribute("isDirectory"))) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (!dstPath.exists()) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts try {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts mkdirs(dstPath);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } catch (FileAlreadyExistsException x) {}
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = srcPath.newDirectoryStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (Path child : ds) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts z2zcopy(src, dst,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path + (path.endsWith("/")?"":"/") + child.getName());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ds.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } else {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //System.out.println("copying..." + path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts srcPath.copyTo(dstPath);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // use TreeWalk to move
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void z2zmove(FileSystem src, FileSystem dst, String path)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts final Path srcPath = src.getPath(path).toAbsolutePath();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts final Path dstPath = dst.getPath(path).toAbsolutePath();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Files.walkFileTree(srcPath, new SimpleFileVisitor<Path>() {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts @Override
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult visitFile(Path file,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts BasicFileAttributes attrs)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path dst = srcPath.relativize(file);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = dstPath.resolve(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts try {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path parent = dstPath.getParent();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (parent != null && parent.notExists())
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts mkdirs(parent);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts file.moveTo(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } catch (IOException x) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts x.printStackTrace();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return FileVisitResult.CONTINUE;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts @Override
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult preVisitDirectory(Path dir,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts BasicFileAttributes attrs)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path dst = srcPath.relativize(dir);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dst = dstPath.resolve(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts try {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (dst.notExists())
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts mkdirs(dst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } catch (IOException x) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts x.printStackTrace();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return FileVisitResult.CONTINUE;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts @Override
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult postVisitDirectory(Path dir,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts IOException ioe)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts try {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts dir.delete();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } catch (IOException x) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //x.printStackTrace();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return FileVisitResult.CONTINUE;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts });
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void mkdirs(Path path) throws IOException {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path = path.toAbsolutePath();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path parent = path.getParent();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (parent != null) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (parent.notExists())
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts mkdirs(parent);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path.createDirectory();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void rmdirs(Path path) throws IOException {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while (path != null && path.getNameCount() != 0) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path.delete();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path = path.getParent();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void list(Path path, boolean verbose ) throws IOException {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (!"/".equals(path.toString())) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf(" %s%n", path.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (verbose)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println(Attributes.readBasicFileAttributes(path).toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (path.notExists())
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (Attributes.readBasicFileAttributes(path).isDirectory()) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = path.newDirectoryStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts for (Path child : ds)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts list(child, verbose);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ds.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // check the content of two paths are equal
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void checkEqual(Path src, Path dst) throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // src.toString(), dst.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //streams
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts InputStream isSrc = src.newInputStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts InputStream isDst = dst.newInputStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts byte[] bufSrc = new byte[8192];
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts byte[] bufDst = new byte[8192];
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts try {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts int nSrc = 0;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while ((nSrc = isSrc.read(bufSrc)) != -1) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts int nDst = 0;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while (nDst < nSrc) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts int n = isDst.read(bufDst, nDst, nSrc - nDst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (n == -1) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.toString(), dst.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts nDst += n;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while (--nSrc >= 0) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (bufSrc[nSrc] != bufDst[nSrc]) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.toString(), dst.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts nSrc--;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } finally {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts isSrc.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts isDst.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // channels
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts SeekableByteChannel chSrc = src.newByteChannel();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts SeekableByteChannel chDst = dst.newByteChannel();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (chSrc.size() != chDst.size()) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("src[%s].size=%d, dst[%s].size=%d%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts chSrc.toString(), chSrc.size(),
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts chDst.toString(), chDst.size());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ByteBuffer bbSrc = ByteBuffer.allocate(8192);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ByteBuffer bbDst = ByteBuffer.allocate(8192);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts try {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts int nSrc = 0;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while ((nSrc = chSrc.read(bbSrc)) != -1) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts int nDst = chDst.read(bbDst);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (nSrc != nDst) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.toString(), dst.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while (--nSrc >= 0) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts src.toString(), dst.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts nSrc--;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts bbSrc.flip();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts bbDst.flip();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } catch (IOException x) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts x.printStackTrace();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts } finally {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts chSrc.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts chDst.close();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void fchCopy(Path src, Path dst) throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Set<OpenOption> read = new HashSet<>();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts read.add(READ);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Set<OpenOption> openwrite = new HashSet<>();
openwrite.add(CREATE_NEW);
openwrite.add(WRITE);
FileChannel srcFc = src.getFileSystem()
.provider()
.newFileChannel(src, read);
FileChannel dstFc = dst.getFileSystem()
.provider()
.newFileChannel(dst, openwrite);
try {
ByteBuffer bb = ByteBuffer.allocate(8192);
while (srcFc.read(bb) >= 0) {
bb.flip();
dstFc.write(bb);
bb.clear();
}
} finally {
srcFc.close();
dstFc.close();
}
}
private static void chCopy(Path src, Path dst) throws IOException
{
Set<OpenOption> read = new HashSet<>();
read.add(READ);
Set<OpenOption> openwrite = new HashSet<>();
openwrite.add(CREATE_NEW);
openwrite.add(WRITE);
SeekableByteChannel srcCh = src.newByteChannel(read);
SeekableByteChannel dstCh = dst.newByteChannel(openwrite);
try {
ByteBuffer bb = ByteBuffer.allocate(8192);
while (srcCh.read(bb) >= 0) {
bb.flip();
dstCh.write(bb);
bb.clear();
}
} finally {
srcCh.close();
dstCh.close();
}
}
private static void streamCopy(Path src, Path dst) throws IOException
{
InputStream isSrc = src.newInputStream();
OutputStream osDst = dst.newOutputStream();
byte[] buf = new byte[8192];
try {
int n = 0;
while ((n = isSrc.read(buf)) != -1) {
osDst.write(buf, 0, n);
}
} finally {
isSrc.close();
osDst.close();
}
}
}