列出指定的zip文件的内容
//package com.nowjava; import java.io.IOException;//n o w j a v a . c o m - 时代Java import java.net.URI; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; /* from N o w J a v a . c o m - 时代Java */ public class Main { /** * List the contents of the specified zip file * * @param filename * @throws IOException * @throws URISyntaxException */ public static void list(String zipFilename) throws IOException { System.out.printf("Listing Archive: %s\n", zipFilename); //create the file system try (FileSystem zipFileSystem = createZipFileSystem(zipFilename, false)) { final Path root = zipFileSystem.getPath("/"); //walk the file tree and print out the directory and filenames Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { print(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { print(dir); return FileVisitResult.CONTINUE; } /** * prints out details about the specified path such as size and modification time * * @param file * @throws IOException */ private void print(Path file) throws IOException { final DateFormat df = new SimpleDateFormat( "MM/dd/yyyy-HH:mm:ss"); final String modTime = df.format(new Date(Files .getLastModifiedTime(file).toMillis())); System.out.printf("%d %s %s\n", Files.size(file), modTime, file); } }); } } /** * Returns a zip file system * * @param zipFilename to construct the file system from * @param create <code>true</code> if the zip file should be created * @return a zip file system * @throws IOException */ public static FileSystem createZipFileSystem(String zipFilename, boolean create) throws IOException { final Path path = Paths.get(zipFilename); final URI uri = URI.create("jar:file:" + path.toUri().getPath()); final Map<String, String> env = new HashMap<>(); if (create) { env.put("create", "true"); } return FileSystems.newFileSystem(uri, env); } /** * Creates/updates a zip file. * * @param zipFilename the name of the zip to create * @param filenames list of filename to add to the zip * @throws IOException */ public static void create(String zipFilename, String... filenames) throws IOException { try (FileSystem zipFileSystem = createZipFileSystem(zipFilename, true)) { final Path root = zipFileSystem.getPath("/"); //iterate over the files we need to add for (String filename : filenames) { final Path src = Paths.get(filename); //add a file to the zip file system if (!Files.isDirectory(src)) { final Path dest = zipFileSystem.getPath( root.toString(), src.toString()); final Path parent = dest.getParent(); if (Files.notExists(parent)) { System.out .printf("Creating directory %s\n", parent); Files.createDirectories(parent); } Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING); } else { //for directories, walk the file tree Files.walkFileTree(src, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { final Path dest = zipFileSystem.getPath( root.toString(), fi