0

I am trying to list files in a zip folder. I understand i can use the zip4j library or the java ZipFile class.

My question is using either of these tools, how can I list files in a given directory within the zip?

Maclaren
  • 259
  • 3
  • 13

1 Answers1

1

It is possible to solve the task with just 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());
        }
    }
}
surlac
  • 2,961
  • 2
  • 22
  • 31