我正在尝试列出 zip 文件夹中的文件。我知道我可以使用 zip4j 库或 javaZipFile
类。
我的问题是使用这些工具中的任何一个,如何列出 zip 中给定目录中的文件?
只需ZipInputStream即可解决该任务。
final String folderNameToBrowse = "folder_name";
final String archivePath = "archive.zip";
final Path pathToBrowse = Paths.get(folderNameToBrowse);
ZipInputStream zis = new ZipInputStream(new FileInputStream(archivePath));
ZipEntry temp = null;
while ( (temp = zis.getNextEntry()) != null) {
if(!temp.isDirectory()){
Path currentPath = Paths.get(temp.getName());
if(pathToBrowse.equals(currentPath.getParent())){
// Here is the file name
System.out.println(currentPath.getFileName());
}
}
}