List of .zip directories without extraction

I create a file explorer in Java and I list the files / folders in JTrees. What I'm trying to do now is when I get to the zipped folder that I want to list, but not extracting it first.

If anyone has an idea, share it.

+4
source share
3 answers

I suggest you take a look at ZipFile.entries() .

Here is the code:

 try (ZipFile zipFile = new ZipFile("test.zip")) { Enumeration zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { String fileName = ((ZipEntry) zipEntries.nextElement()).getName(); System.out.println(fileName); } } 

If you are using Java 8 , you can avoid using the almost obsolete Enumeration class by using ZipFile::stream as follows:

 zipFile.stream() .map(ZipEntry::getName) .forEach(System.out::println); 

If you need to know if the entry is a directory or not, you can use ZipEntry.isDirectory . You cannot get much more information than without extracting the file (for obvious reasons).


If you want to not extract all the files, you can extract one file at a time using ZipFile.getInputStream for each ZipEntry . (Keep in mind that you do not need to store the decompressed data on disk, you can just read the input stream and discard the bytes in the process.

+13
source

Use the java.util.zip.ZipFile class and, in particular, its entries method.

You will have something like this:

 ZipFile zipFile = new ZipFile("testfile.zip"); Enumeration zipEntries = zipFile.entries(); String fname; while (zipEntries.hasMoreElements()) { fname = ((ZipEntry)zipEntries.nextElement()).getName(); ... } 
+8
source

You can use the ZipFile class to process ZIP files. It has an entries () method that returns a list of entries contained in a ZIP file. This information is contained in the ZIP header, and extraction is not required.

+1
source

All Articles