Maclaren
Maclaren

Reputation: 289

Android list files in specific zip folder

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?

Upvotes: 0

Views: 780

Answers (1)

surlac
surlac

Reputation: 2961

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());
        }
    }
}

Upvotes: 1

Related Questions