43 import java.nio.channels.*;
44 import java.nio.file.*;
45 import java.nio.file.spi.*;
46 import java.nio.file.attribute.*;
48 import java.text.DateFormat;
49 import java.text.SimpleDateFormat;
52 import static java.nio.file.StandardOpenOption.*;
53 import static java.nio.file.StandardCopyOption.*;
155 public static void main(String[] args)
throws Throwable {
157 if (provider ==
null) {
158 System.err.println(
"ZIP filesystem provider is not installed");
162 Action action = Action.valueOf(args[0]);
163 Map<String, Object> env = env =
new HashMap<>();
164 if (action == Action.create)
165 env.put(
"create",
"true");
166 try (FileSystem fs = provider.newFileSystem(Paths.get(args[1]), env)) {
170 src = fs.getPath(args[2]);
171 dst = fs.getPath(args[3]);
172 Files.move(src, dst);
175 src = fs.getPath(args[2]);
176 dst = Paths.get(args[3]);
177 Files.move(src, dst);
180 src = Paths.get(args[2]);
181 dst = fs.getPath(args[3]);
182 Files.move(src, dst);
185 src = fs.getPath(args[2]);
186 dst = fs.getPath(args[3]);
187 Files.copy(src, dst);
190 src = fs.getPath(args[2]);
191 dst = Paths.get(args[3]);
192 Files.copy(src, dst);
195 src = Paths.get(args[2]);
196 dst = fs.getPath(args[3]);
197 Files.copy(src, dst);
200 src = Paths.get(args[2]);
201 dst = fs.getPath(args[3]);
202 Files.copy(src, dst, COPY_ATTRIBUTES);
205 src = fs.getPath(args[2]);
206 dst = Paths.get(args[3]);
207 Files.copy(src, dst, COPY_ATTRIBUTES);
210 try (FileSystem fs2 = provider.newFileSystem(Paths.get(args[2]), env)) {
215 try (FileSystem fs2 = provider.newFileSystem(Paths.get(args[2]), env)) {
220 for (
int i = 2; i < args.length; i++) {
221 path = fs.getPath(args[i]);
222 System.out.println(path);
224 Files.readAttributes(path, BasicFileAttributes.class).toString());
228 DateFormat df =
new SimpleDateFormat(
"MM/dd/yyyy-HH:mm:ss");
229 Date newDatetime = df.parse(args[2]);
230 for (
int i = 3; i < args.length; i++) {
231 path = fs.getPath(args[i]);
232 Files.setAttribute(path,
"lastModifiedTime",
233 FileTime.fromMillis(newDatetime.getTime()));
235 Files.readAttributes(path, BasicFileAttributes.class).toString());
239 df =
new SimpleDateFormat(
"MM/dd/yyyy-HH:mm:ss");
240 newDatetime = df.parse(args[2]);
241 for (
int i = 3; i < args.length; i++) {
242 path = fs.getPath(args[i]);
243 Files.setAttribute(path,
"creationTime",
244 FileTime.fromMillis(newDatetime.getTime()));
246 Files.readAttributes(path, BasicFileAttributes.class).toString());
250 df =
new SimpleDateFormat(
"MM/dd/yyyy-HH:mm:ss");
251 newDatetime = df.parse(args[2]);
252 for (
int i = 3; i < args.length; i++) {
253 path = fs.getPath(args[i]);
254 Files.setAttribute(path,
"lastAccessTime",
255 FileTime.fromMillis(newDatetime.getTime()));
257 Files.readAttributes(path, BasicFileAttributes.class).toString());
261 path = fs.getPath(
"/");
262 FileStore fstore = Files.getFileStore(path);
263 System.out.printf(
"filestore[%s]%n", fstore.name());
264 System.out.printf(
" totalSpace: %d%n",
265 (Long)fstore.getAttribute(
"totalSpace"));
266 System.out.printf(
" usableSpace: %d%n",
267 (Long)fstore.getAttribute(
"usableSpace"));
268 System.out.printf(
" unallocSpace: %d%n",
269 (Long)fstore.getAttribute(
"unallocatedSpace"));
274 list(fs.getPath(
"/"),
false);
276 list(fs.getPath(args[2]),
false);
280 list(fs.getPath(
"/"),
true);
282 list(fs.getPath(args[2]),
true);
286 walk(fs.getPath((args.length > 2)? args[2] :
"/"));
289 if (args.length == 2) {
292 for (
int i = 2; i < args.length; i++) {
298 for (
int i = 2; i < args.length; i++)
299 Files.delete(fs.getPath(args[i]));
304 for (
int i = 2; i < args.length; i++) {
309 path = fs.getPath(args[2]);
310 final String fStr = (args.length > 3)?args[3]:
"";
311 try (DirectoryStream<Path> ds = Files.newDirectoryStream(path,
312 new DirectoryStream.Filter<Path>() {
314 public boolean accept(Path path) {
315 return path.toString().contains(fStr);
320 System.out.println(p);
324 Files.createDirectory(fs.getPath(args[2]));
327 mkdirs(fs.getPath(args[2]));
330 for (
int i = 2; i < args.length; i++) {
331 path = fs.getPath(args[i]);
332 System.out.printf(
"%n%s%n", path);
333 System.out.println(
"-------(1)---------");
335 Files.readAttributes(path, BasicFileAttributes.class).toString());
336 System.out.println(
"-------(2)---------");
337 Map<String, Object> map = Files.readAttributes(path,
"zip:*");
338 for (Map.Entry<String, Object> e : map.entrySet()) {
339 System.out.printf(
" %s : %s%n", e.getKey(), e.getValue());
341 System.out.println(
"-------(3)---------");
342 map = Files.readAttributes(path,
"size,lastModifiedTime,isDirectory");
343 for (Map.Entry<String, ?> e : map.entrySet()) {
344 System.out.printf(
" %s : %s%n", e.getKey(), e.getValue());
349 list(fs.getPath(
"/"),
false);
353 System.out.println(
"sleeping...");
356 }
catch (Exception x) {
362 for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
363 if (
"jar".equals(provider.getScheme()))
369 @SuppressWarnings(
"unused")
373 private static
byte[] getBytes(String name) {
374 return name.getBytes();
377 @SuppressWarnings(
"unused")
381 private static String getString(
byte[] name) {
382 return new String(name);
385 private static void walk(Path path)
throws IOException
389 new SimpleFileVisitor<Path>() {
390 private int indent = 0;
391 private void indent() {
394 System.out.printf(
" ");
398 public FileVisitResult visitFile(Path file,
399 BasicFileAttributes attrs)
402 System.out.printf(
"%s%n", file.getFileName().toString());
403 return FileVisitResult.CONTINUE;
407 public FileVisitResult preVisitDirectory(Path dir,
408 BasicFileAttributes attrs)
411 System.out.printf(
"[%s]%n", dir.toString());
413 return FileVisitResult.CONTINUE;
417 public FileVisitResult postVisitDirectory(Path dir,
421 return FileVisitResult.CONTINUE;
426 private static void update(FileSystem fs, String path)
throws Throwable{
427 Path src = FileSystems.getDefault().getPath(path);
428 if (Files.isDirectory(src)) {
429 try (DirectoryStream<Path> ds = Files.newDirectoryStream(src)) {
430 for (Path child : ds)
431 update(fs, child.toString());
434 Path dst = fs.getPath(path);
435 Path parent = dst.getParent();
436 if (parent !=
null && Files.notExists(parent))
438 Files.copy(src, dst, REPLACE_EXISTING);
442 private static void extract(FileSystem fs, String path)
throws Throwable{
443 Path src = fs.getPath(path);
444 if (Files.isDirectory(src)) {
445 try (DirectoryStream<Path> ds = Files.newDirectoryStream(src)) {
446 for (Path child : ds)
447 extract(fs, child.toString());
450 if (path.startsWith(
"/"))
451 path = path.substring(1);
452 Path dst = FileSystems.getDefault().getPath(path);
453 Path parent = dst.getParent();
454 if (Files.notExists(parent))
456 Files.copy(src, dst, REPLACE_EXISTING);
461 private static void z2zcopy(FileSystem src, FileSystem dst, String path)
464 Path srcPath = src.getPath(path);
465 Path dstPath = dst.getPath(path);
467 if (Files.isDirectory(srcPath)) {
468 if (!Files.exists(dstPath)) {
471 }
catch (FileAlreadyExistsException x) {}
473 try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
474 for (Path child : ds) {
476 path + (path.endsWith(
"/")?
"":
"/") + child.getFileName());
481 Files.copy(srcPath, dstPath);
486 private static void z2zmove(FileSystem src, FileSystem dst, String path)
489 final Path srcPath = src.getPath(path).toAbsolutePath();
490 final Path dstPath = dst.getPath(path).toAbsolutePath();
492 Files.walkFileTree(srcPath,
new SimpleFileVisitor<Path>() {
495 public FileVisitResult visitFile(Path file,
496 BasicFileAttributes attrs)
498 Path dst = srcPath.relativize(file);
499 dst = dstPath.resolve(dst);
501 Path parent = dstPath.getParent();
502 if (parent !=
null && Files.notExists(parent))
504 Files.move(file, dst);
505 }
catch (IOException x) {
508 return FileVisitResult.CONTINUE;
512 public FileVisitResult preVisitDirectory(Path dir,
513 BasicFileAttributes attrs)
515 Path dst = srcPath.relativize(dir);
516 dst = dstPath.resolve(dst);
519 if (Files.notExists(dst))
521 }
catch (IOException x) {
524 return FileVisitResult.CONTINUE;
528 public FileVisitResult postVisitDirectory(Path dir,
534 }
catch (IOException x) {
537 return FileVisitResult.CONTINUE;
543 private static void mkdirs(Path path)
throws IOException {
544 path = path.toAbsolutePath();
545 Path parent = path.getParent();
546 if (parent !=
null) {
547 if (Files.notExists(parent))
550 Files.createDirectory(path);
553 @SuppressWarnings(
"unused")
557 private static
void rmdirs(Path path) throws IOException {
558 while (path !=
null && path.getNameCount() != 0) {
560 path = path.getParent();
564 private static void list(Path path,
boolean verbose )
throws IOException {
565 if (!
"/".equals(path.toString())) {
566 System.out.printf(
" %s%n", path.toString());
568 System.out.println(Files.readAttributes(path, BasicFileAttributes.class).toString());
570 if (Files.notExists(path))
572 if (Files.isDirectory(path)) {
573 try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
574 for (Path child : ds)
575 list(child, verbose);
580 @SuppressWarnings(
"unused")
585 private static
void checkEqual(Path src, Path dst) throws IOException
591 byte[] bufSrc =
new byte[8192];
592 byte[] bufDst =
new byte[8192];
593 try (InputStream isSrc = Files.newInputStream(src);
594 InputStream isDst = Files.newInputStream(dst))
597 while ((nSrc = isSrc.read(bufSrc)) != -1) {
599 while (nDst < nSrc) {
600 int n = isDst.read(bufDst, nDst, nSrc - nDst);
602 System.out.printf(
"checking <%s> vs <%s>...%n",
603 src.toString(), dst.toString());
604 throw new RuntimeException(
"CHECK FAILED!");
608 while (--nSrc >= 0) {
609 if (bufSrc[nSrc] != bufDst[nSrc]) {
610 System.out.printf(
"checking <%s> vs <%s>...%n",
611 src.toString(), dst.toString());
612 throw new RuntimeException(
"CHECK FAILED!");
621 try (SeekableByteChannel chSrc = Files.newByteChannel(src);
622 SeekableByteChannel chDst = Files.newByteChannel(dst))
624 if (chSrc.size() != chDst.size()) {
625 System.out.printf(
"src[%s].size=%d, dst[%s].size=%d%n",
626 chSrc.toString(), chSrc.size(),
627 chDst.toString(), chDst.size());
628 throw new RuntimeException(
"CHECK FAILED!");
630 ByteBuffer bbSrc = ByteBuffer.allocate(8192);
631 ByteBuffer bbDst = ByteBuffer.allocate(8192);
634 while ((nSrc = chSrc.read(bbSrc)) != -1) {
635 int nDst = chDst.read(bbDst);
637 System.out.printf(
"checking <%s> vs <%s>...%n",
638 src.toString(), dst.toString());
639 throw new RuntimeException(
"CHECK FAILED!");
641 while (--nSrc >= 0) {
642 if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
643 System.out.printf(
"checking <%s> vs <%s>...%n",
644 src.toString(), dst.toString());
645 throw new RuntimeException(
"CHECK FAILED!");
652 }
catch (IOException x) {
657 private static void fchCopy(Path src, Path dst)
throws IOException
659 Set<OpenOption> read =
new HashSet<>();
661 Set<OpenOption> openwrite =
new HashSet<>();
662 openwrite.add(CREATE_NEW);
663 openwrite.add(WRITE);
665 try (FileChannel srcFc = src.getFileSystem().provider().newFileChannel(src, read);
666 FileChannel dstFc = dst.getFileSystem().provider().newFileChannel(dst, openwrite))
668 ByteBuffer bb = ByteBuffer.allocate(8192);
669 while (srcFc.read(bb) >= 0) {
677 private static void chCopy(Path src, Path dst)
throws IOException
679 Set<OpenOption> read =
new HashSet<>();
681 Set<OpenOption> openwrite =
new HashSet<>();
682 openwrite.add(CREATE_NEW);
683 openwrite.add(WRITE);
685 try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
686 SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
688 ByteBuffer bb = ByteBuffer.allocate(8192);
689 while (srcCh.read(bb) >= 0) {
697 private static void streamCopy(Path src, Path dst)
throws IOException
699 byte[] buf =
new byte[8192];
700 try (InputStream isSrc = Files.newInputStream(src);
701 OutputStream osDst = Files.newOutputStream(dst))
704 while ((n = isSrc.read(buf)) != -1) {
705 osDst.write(buf, 0, n);