本帖最后由 不二晨 于 2018-7-5 10:03 编辑
[size=13.3333px]如果用一般的文件流操作来解压zip文件,稍微会有点麻烦。JavaSE7的java.nio.file包新加了几个新类,我们可以利用他们来简化解压过程。 [size=13.3333px]zip文件名用String变量zipFile来表示。 [size=13.3333px]1 定义一个zip文件的文件系统: [size=13.3333px][size=1em][size=13.3333px]
1
FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile), null);
[size=13.3333px] 2 获取当前目录 [size=13.3333px][size=1em][size=13.3333px]
[size=13.3333px]1
[size=13.3333px] final String currentPath = System.getProperty("user.dir");
[size=13.3333px] 注意,因为稍后在匿名类中会用到变量currentPath,所以需要定义为final [size=13.3333px]3 利用Files的静态方法walkFileTree遍历整个zip文件系统,同时将文件拷贝到指定目录
[size=13.3333px] 3.1 调用getPath来获取zip文件系统的根目录 [size=13.3333px][size=1em][size=13.3333px]1
[size=13.3333px] fs.getPath("/")
[size=13.3333px]
[size=13.3333px] 3.2 new一个SimpleFileVisitor<Path>,便利文件系统时可获取文件的属性。 [size=13.3333px][size=1em][/table]
1
2
new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
[size=13.3333px] 3.3 获取目的地址,其中Paths.get可将两个合法的地址拼接为一个 [size=13.3333px][size=1em]
[size=13.3333px]
[size=13.3333px]1
[size=13.3333px] Path destPath = Paths.get(currentPath, file.toString());
[size=13.3333px]
| [size=13.3333px] 3.4 如果目标文件已经存在,则删除 [size=13.3333px][size=1em] |
1
Files.deleteIfExists(destPath);
[size=13.3333px] 3.5 创建文件的父目录,不管是文件isFile还是isDirectory,按照Linux文件系统的观点,都可以用同一种方式来处理,即首先创建父目录,然后创建文件(文件夹)。 [size=13.3333px][size=1em]
[size=13.3333px]
[size=13.3333px]1
[size=13.3333px] Files.createDirectories(destPath.getParent());
[size=13.3333px][table=98%,none]
[size=13.3333px] 3.6 移动文件 [size=13.3333px][size=1em]
[size=13.3333px]1
[size=13.3333px] Files.move(file, destPath);
[size=13.3333px]import java.io.IOException;
[size=13.3333px]import java.nio.file.FileSystem;
[size=13.3333px]import java.nio.file.FileSystems;
[size=13.3333px]import java.nio.file.FileVisitResult;
[size=13.3333px]import java.nio.file.Files;
[size=13.3333px]import java.nio.file.Path;
[size=13.3333px]import java.nio.file.Paths;
[size=13.3333px]import java.nio.file.SimpleFileVisitor;
[size=13.3333px]import java.nio.file.attribute.BasicFileAttributes;
[size=13.3333px]
[size=13.3333px]public class Zipper {
[size=13.3333px] public static void main(String[] args) throws Exception {
[size=13.3333px] if (args.length == 0) {
[size=13.3333px] System.out.println("Usage: java Zipper zipfilename");
[size=13.3333px] System.exit(0);
[size=13.3333px] }
[size=13.3333px]
[size=13.3333px] unzip(args[0]);
[size=13.3333px] }
[size=13.3333px]
[size=13.3333px] public static void unzip(String zipFile) throws Exception {
[size=13.3333px] FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile), null);
[size=13.3333px] final String currentPath = System.getProperty("user.dir");
[size=13.3333px] System.out.println("current directory:" + currentPath);
[size=13.3333px]
[size=13.3333px] Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>() {
[size=13.3333px] public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
[size=13.3333px] Path destPath = Paths.get(currentPath, file.toString());
[size=13.3333px] Files.deleteIfExists(destPath);
[size=13.3333px] Files.createDirectories(destPath.getParent());
[size=13.3333px] Files.move(file, destPath);
[size=13.3333px] return FileVisitResult.CONTINUE;
[size=13.3333px] }
[size=13.3333px] });
[size=13.3333px] }
[size=13.3333px]}
【转载】出处:https://www.cnblogs.com/lyndon-chen/p/3575393.html
|