java遍历zip文件

java原始代码实现不解压zip压缩包情况下,对zip文件内所有文件进行遍历

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;


public class ZipTest {
    public static void main(String[] args) throws IOException {
        // zip文件
        Path zipPath = Paths.get("D:\\nginx-1.12.2.zip");
        FileSystem fileSystems = FileSystems.newFileSystem(zipPath, null);
        // zip文件下根目录 如要指定目录可配置指定的目录 如:/nginx-1.12.2/conf
        Path startPath = fileSystems.getPath("/");
        // 遍历zip包
        Files.walkFileTree(startPath, new SimpleFileVisitor() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println();
                System.out.println(file);
                return FileVisitResult.CONTINUE;
            }

        });
    }
}

zip文件内容:

java遍历zip文件_第1张图片

 

代码运行结果如下:

java遍历zip文件_第2张图片

 

你可能感兴趣的:(java,开发语言,后端)