java loop through zip file

The original java code implements traversal of all files in the zip file without decompressing the zip archive

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<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println();
                System.out.println(file);
                return FileVisitResult.CONTINUE;
            }

        });
    }
}

zip file content:

 

The result of running the code is as follows:

 

Guess you like

Origin blog.csdn.net/idto315/article/details/122815094