/* * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
create, // <java Demo create zipfile file [...]> // create a new zipfile if it doesn't exit // and then add the file(s) into it.
attrs2, // <java Demo attrs2 zipfile file [...]> // test different ways to print attrs
prof,
}
publicstaticvoid main(String[] args) throws Throwable {
FileSystemProvider provider = getZipFSProvider(); if (provider == null) {
System.err.println("ZIP filesystem provider is not installed");
System.exit(1);
}
Action action = Action.valueOf(args[0]);
Map<String, Object> env = new HashMap<>(); if (action == Action.create)
env.put("create", "true"); try (FileSystem fs = provider.newFileSystem(Paths.get(args[1]), env)) {
Path path, src, dst; switch (action) { case rename:
src = fs.getPath(args[2]);
dst = fs.getPath(args[3]);
Files.move(src, dst); break; case moveout:
src = fs.getPath(args[2]);
dst = Paths.get(args[3]);
Files.move(src, dst); break; case movein:
src = Paths.get(args[2]);
dst = fs.getPath(args[3]);
Files.move(src, dst); break; case copy:
src = fs.getPath(args[2]);
dst = fs.getPath(args[3]);
Files.copy(src, dst); break; case copyout:
src = fs.getPath(args[2]);
dst = Paths.get(args[3]);
Files.copy(src, dst); break; case copyin:
src = Paths.get(args[2]);
dst = fs.getPath(args[3]);
Files.copy(src, dst); break; case copyin_attrs:
src = Paths.get(args[2]);
dst = fs.getPath(args[3]);
Files.copy(src, dst, COPY_ATTRIBUTES); break; case copyout_attrs:
src = fs.getPath(args[2]);
dst = Paths.get(args[3]);
Files.copy(src, dst, COPY_ATTRIBUTES); break; case zzmove: try (FileSystem fs2 = provider.newFileSystem(Paths.get(args[2]), env)) {
z2zmove(fs, fs2, args[3]);
} break; case zzcopy: try (FileSystem fs2 = provider.newFileSystem(Paths.get(args[2]), env)) {
z2zcopy(fs, fs2, args[3]);
} break; case attrs: for (int i = 2; i < args.length; i++) {
path = fs.getPath(args[i]);
System.out.println(path);
System.out.println(
Files.readAttributes(path, BasicFileAttributes.class).toString());
} break; case setmtime:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
Date newDatetime = df.parse(args[2]); for (int i = 3; i < args.length; i++) {
path = fs.getPath(args[i]);
Files.setAttribute(path, "lastModifiedTime",
FileTime.fromMillis(newDatetime.getTime()));
System.out.println(
Files.readAttributes(path, BasicFileAttributes.class).toString());
} break; case setctime:
df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
newDatetime = df.parse(args[2]); for (int i = 3; i < args.length; i++) {
path = fs.getPath(args[i]);
Files.setAttribute(path, "creationTime",
FileTime.fromMillis(newDatetime.getTime()));
System.out.println(
Files.readAttributes(path, BasicFileAttributes.class).toString());
} break; case setatime:
df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
newDatetime = df.parse(args[2]); for (int i = 3; i < args.length; i++) {
path = fs.getPath(args[i]);
Files.setAttribute(path, "lastAccessTime",
FileTime.fromMillis(newDatetime.getTime()));
System.out.println(
Files.readAttributes(path, BasicFileAttributes.class).toString());
} break; case attrsspace:
path = fs.getPath("/");
FileStore fstore = Files.getFileStore(path);
System.out.printf("filestore[%s]%n", fstore.name());
System.out.printf(" totalSpace: %d%n",
(Long)fstore.getAttribute("totalSpace"));
System.out.printf(" usableSpace: %d%n",
(Long)fstore.getAttribute("usableSpace"));
System.out.printf(" unallocSpace: %d%n",
(Long)fstore.getAttribute("unallocatedSpace")); break; case list: case tlist: if (args.length < 3)
list(fs.getPath("/"), false); else
list(fs.getPath(args[2]), false); break; case vlist: if (args.length < 3)
list(fs.getPath("/"), true); else
list(fs.getPath(args[2]), true); break; case twalk: case walk:
walk(fs.getPath((args.length > 2)? args[2] : "/")); break; case extract: if (args.length == 2) {
extract(fs, "/");
} else { for (int i = 2; i < args.length; i++) {
extract(fs, args[i]);
}
} break; casedelete: for (int i = 2; i < args.length; i++)
Files.delete(fs.getPath(args[i])); break; case create: case add: case update: for (int i = 2; i < args.length; i++) {
update(fs, args[i]);
} break; case lsdir:
path = fs.getPath(args[2]); final String fStr = (args.length > 3)?args[3]:""; try (DirectoryStream<Path> ds = Files.newDirectoryStream(path, new DirectoryStream.Filter<Path>() {
@Override publicboolean accept(Path path) { return path.toString().contains(fStr);
}
}))
{ for (Path p : ds)
System.out.println(p);
} break; case mkdir:
Files.createDirectory(fs.getPath(args[2])); break; case mkdirs:
mkdirs(fs.getPath(args[2])); break; case attrs2: for (int i = 2; i < args.length; i++) {
path = fs.getPath(args[i]);
System.out.printf("%n%s%n", path);
System.out.println("-------(1)---------");
System.out.println(
Files.readAttributes(path, BasicFileAttributes.class).toString());
System.out.println("-------(2)---------");
Map<String, Object> map = Files.readAttributes(path, "zip:*"); for (Map.Entry<String, Object> e : map.entrySet()) {
System.out.printf(" %s : %s%n", e.getKey(), e.getValue());
}
System.out.println("-------(3)---------");
map = Files.readAttributes(path, "size,lastModifiedTime,isDirectory"); for (Map.Entry<String, ?> e : map.entrySet()) {
System.out.printf(" %s : %s%n", e.getKey(), e.getValue());
}
} break; case prof:
list(fs.getPath("/"), false); while (true) { Thread.sleep(10000); //list(fs.getPath("/"), true);
System.out.println("sleeping...");
}
}
} catch (Exception x) {
x.printStackTrace();
}
}
privatestatic FileSystemProvider getZipFSProvider() { for (FileSystemProvider provider : FileSystemProvider.installedProviders()) { if ("jar".equals(provider.getScheme())) return provider;
} returnnull;
}
@SuppressWarnings("unused") /** * Not used in demo, but included for demonstrational purposes.
*/ privatestaticbyte[] getBytes(String name) { return name.getBytes();
}
@SuppressWarnings("unused") /** * Not used in demo, but included for demonstrational purposes.
*/ privatestatic String getString(byte[] name) { returnnew String(name);
}
privatestaticvoid walk(Path path) throws IOException
{
Files.walkFileTree(
path, new SimpleFileVisitor<Path>() { privateint indent = 0; privatevoid indent() { int n = 0; while (n++ < indent)
System.out.printf(" ");
}
/** * Not used in demo, but included for demonstrational purposes.
*/
@SuppressWarnings("unused") privatestaticvoid rmdirs(Path path) throws IOException { while (path != null && path.getNameCount() != 0) {
Files.delete(path);
path = path.getParent();
}
}
privatestaticvoid list(Path path, boolean verbose ) throws IOException { if (!"/".equals(path.toString())) {
System.out.printf(" %s%n", path.toString()); if (verbose)
System.out.println(Files.readAttributes(path, BasicFileAttributes.class).toString());
} if (Files.notExists(path)) return; if (Files.isDirectory(path)) { try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) { for (Path child : ds)
list(child, verbose);
}
}
}
/** * Checks that the content of two paths are equal. * Not used in demo, but included for demonstrational purposes.
*/
@SuppressWarnings("unused") privatestaticvoid checkEqual(Path src, Path dst) throws IOException
{ //System.out.printf("checking <%s> vs <%s>...%n", // src.toString(), dst.toString());
//streams byte[] bufSrc = newbyte[8192]; byte[] bufDst = newbyte[8192]; try (InputStream isSrc = Files.newInputStream(src);
InputStream isDst = Files.newInputStream(dst))
{ int nSrc = 0; while ((nSrc = isSrc.read(bufSrc)) != -1) { int nDst = 0; while (nDst < nSrc) { int n = isDst.read(bufDst, nDst, nSrc - nDst); if (n == -1) {
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString()); thrownew RuntimeException("CHECK FAILED!");
}
nDst += n;
} while (--nSrc >= 0) { if (bufSrc[nSrc] != bufDst[nSrc]) {
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString()); thrownew RuntimeException("CHECK FAILED!");
}
nSrc--;
}
}
}
int nSrc = 0; while ((nSrc = chSrc.read(bbSrc)) != -1) { int nDst = chDst.read(bbDst); if (nSrc != nDst) {
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString()); thrownew RuntimeException("CHECK FAILED!");
} while (--nSrc >= 0) { if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString()); thrownew RuntimeException("CHECK FAILED!");
}
nSrc--;
}
bbSrc.flip();
bbDst.flip();
}
} catch (IOException x) {
x.printStackTrace();
}
}
/** * Not used in demo, but included for demonstrational purposes.
*/
@SuppressWarnings("unused") privatestaticvoid fchCopy(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);
/** * Not used in demo, but included for demonstrational purposes.
*/
@SuppressWarnings("unused") privatestaticvoid 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);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.