Demo.java revision 3216
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
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 * - Redistributions of source code must retain the above copyright
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * notice, this list of conditions and the following disclaimer.
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.
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 * 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 Phillpottsimport static java.nio.file.StandardOpenOption.*;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport static java.nio.file.StandardCopyOption.*;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * ZipFileSystem usage demo
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * java Demo action ZipfileName [...]
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * @author Xueming Shen
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottspublic class Demo {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts rename, // <java Demo rename zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // rename entry src to dst inside zipfile
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts movein, // <java Demo movein zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // move an external src file into zipfile
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // as entry dst
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // move a zipfile entry src out to dst
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste // copy entry src to dst inside zipfile
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 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 copyout, // <java Demo copyout zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // copy zipfile entry src" out to file dst
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts copyout_attrs, // <java Demo copyout_attrs zipfile src dst>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts zzmove, // <java Demo zzmove zfsrc zfdst path>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // move entry path/dir from zfsrc to zfdst
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // copy path from zipfile zfsrc to zipfile
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste // printout the attributes of entry path
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste attrsspace, // <java Demo attrsspace zipfile path>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // printout the storespace attrs of entry path
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste setmtime, // <java Demo setmtime zipfile "MM/dd/yy-HH:mm:ss" path...>
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // set the lastModifiedTime of entry path
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 // list dir's direct child files/dirs
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste // recursively list all entries of dir
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // via DirectoryStream
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // list with buildDirTree=true
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // recursively verbose list all entries of
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // dir via DirectoryStream
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // recursively walk all entries of dir
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // via Files.walkFileTree
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // walk with buildDirTree=true
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts extract, // <java Demo extract zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts update, // <java Demo extract zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts delete, // <java Demo delete zipfile file [...]>
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 attrs2, // <java Demo attrs2 zipfile file [...]>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // test different ways to print attrs
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public static void main(String[] args) throws Throwable {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Map<String, Object> env = env = new HashMap<>();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (action == Action.tlist || action == Action.twalk)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts FileSystem fs = FileSystems.newFileSystem(Paths.get(args[1]), env, null);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts boolean isRename = false;
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs2 = FileSystems.newFileSystem(Paths.get(args[2]), env, null);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //sf1.getPath(args[3]).moveTo(fs2.getPath(args[3]));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts fs2 = FileSystems.newFileSystem(Paths.get(args[2]), env, null);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //sf1.getPath(args[3]).copyTo(fs2.getPath(args[3]));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Attributes.readBasicFileAttributes(path).toString());
74b502cf428d057e78572e9fd66fb2f1861887b0Jaco Jooste DateFormat df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts FileTime.fromMillis(newDatetime.getTime()));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Attributes.readBasicFileAttributes(path).toString());
74b502cf428d057e78572e9fd66fb2f1861887b0Jaco Jooste df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts FileTime.fromMillis(newDatetime.getTime()));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Attributes.readBasicFileAttributes(path).toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts FileTime.fromMillis(newDatetime.getTime()));
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste Attributes.readBasicFileAttributes(path).toString());
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste //System.out.println(fstore.getFileStoreAttributeView(FileStoreSpaceAttributeView.class)
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste // .readAttributes());
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste System.out.printf("filestore[%s]%n", fstore.name());
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste (Long)fstore.getAttribute("space:usableSpace"));
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste (Long)fstore.getAttribute("space:unallocatedSpace"));
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste walk(fs.getPath((args.length > 2)? args[2] : "/"));
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts final String fStr = (args.length > 3)?args[3]:"";
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = path.newDirectoryStream(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Attributes.readBasicFileAttributes(path).toString());
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 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 while (true) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //list(fs.getPath("/"), true);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static byte[] getBytes(String name) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static String getString(byte[] name) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void walk(Path path) throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private void indent() {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult visitFile(Path file,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("%s%n", file.getName().toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult preVisitDirectory(Path dir,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("[%s]%n", dir.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult postVisitDirectory(Path dir,
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 private static void extract(FileSystem fs, String path) throws Throwable{
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (Boolean.TRUE.equals(src.getAttribute("isDirectory"))) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = src.newDirectoryStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Path dst = FileSystems.getDefault().getPath(path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // use DirectoryStream
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void z2zcopy(FileSystem src, FileSystem dst, String path)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (Boolean.TRUE.equals(srcPath.getAttribute("isDirectory"))) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = srcPath.newDirectoryStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts path + (path.endsWith("/")?"":"/") + child.getName());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //System.out.println("copying..." + path);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // use TreeWalk to move
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void z2zmove(FileSystem src, FileSystem dst, String path)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts final Path srcPath = src.getPath(path).toAbsolutePath();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts final Path dstPath = dst.getPath(path).toAbsolutePath();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Files.walkFileTree(srcPath, new SimpleFileVisitor<Path>() {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult visitFile(Path file,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult preVisitDirectory(Path dir,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts public FileVisitResult postVisitDirectory(Path dir,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //x.printStackTrace();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void mkdirs(Path path) throws IOException {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void rmdirs(Path path) throws IOException {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while (path != null && path.getNameCount() != 0) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void list(Path path, boolean verbose ) throws IOException {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf(" %s%n", path.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.println(Attributes.readBasicFileAttributes(path).toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts if (Attributes.readBasicFileAttributes(path).isDirectory()) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts DirectoryStream<Path> ds = path.newDirectoryStream();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // check the content of two paths are equal
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void checkEqual(Path src, Path dst) throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts //System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts // src.toString(), dst.toString());
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts while ((nSrc = isSrc.read(bufSrc)) != -1) {
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 throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts SeekableByteChannel chSrc = src.newByteChannel();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts SeekableByteChannel chDst = dst.newByteChannel();
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("src[%s].size=%d, dst[%s].size=%d%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ByteBuffer bbSrc = ByteBuffer.allocate(8192);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ByteBuffer bbDst = ByteBuffer.allocate(8192);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts System.out.printf("checking <%s> vs <%s>...%n",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new RuntimeException("CHECK FAILED!");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts private static void fchCopy(Path src, Path dst) throws IOException
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts Set<OpenOption> openwrite = new HashSet<>();
.provider()
.provider()