How to use ZipFile Class in Java to recursively open all files including those under Folders

aruuuuu

I am creating a method in Java to open a zipfile and process Excel files in the zip dynamically. I am using the API ZipFile in Java and would like to process the zipfile as is in memory without extracting it to the file system.

So far I am able to iterate through the zip file but am having trouble listing the files underneath a directory in the zip file. Excel files can be in a folder in the zip file. Below is my current code with a comment in the section that I am having trouble with. Any help is greatly appreciated :)

public static void main(String[] args) {
    try {
        ZipFile zip = new ZipFile(new File("C:\\sample.zip"));
        for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) e.nextElement();

            String currentEntry = entry.getName();

            if (entry.isDirectory()) {
                /*I do not know how to get the files underneath the directory
                  so that I can process them */
                InputStream is = zip.getInputStream(entry);
            } else {
                InputStream is = zip.getInputStream(entry);
            }
        }
    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Jin Kwon

Please take a look on here and here

public static void unzip(final ZipFile zipfile, final File directory)
    throws IOException {

    final Enumeration<? extends ZipEntry> entries = zipfile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        final File file = file(directory, entry);
        if (entry.isDirectory()) {
            continue;
        }
        final InputStream input = zipfile.getInputStream(entry);
        try {
            // copy bytes from input to file
        } finally {
            input.close();
        }
    }
}
protected static File file(final File root, final ZipEntry entry)
    throws IOException {

    final File file = new File(root, entry.getName());

    File parent = file;
    if (!entry.isDirectory()) {
        final String name = entry.getName();
        final int index = name.lastIndexOf('/');
        if (index != -1) {
            parent = new File(root, name.substring(0, index));
        }
    }
    if (parent != null && !parent.isDirectory() && !parent.mkdirs()) {
        throw new IOException(
            "failed to create a directory: " + parent.getPath());
    }

    return file;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use ZipFile Class in Java to recursively open all files including those under Folders

From Dev

(Batch) How to recursively delete all files/folders in a directory except those with a leading .?

From Dev

How to display all files under folders in treeview

From Dev

Command to remove all files and folders recursively including, hidden ones, on MAC OS Terminal?

From Dev

How can we list all files and folders recursively?

From Dev

recursively delete all files, empty directories, and directories with files of multiple names under current directory, including current directory

From Dev

How to use Terminal to delete all .svn folders recursively?

From Dev

zip all files and folders recursively in bash

From Dev

How to recursively delete files and folders under a certain directory using command line?

From Dev

How to recursively search a directory for all files including hidden files in hidden directories, with PowerShell?

From Dev

How to rename folders and files recursively in MacOS

From Dev

How to find all empty files and folders in a specific directory including files which just look empty but are not?

From Dev

How to find all empty files and folders in a specific directory including files which just look empty but are not?

From Dev

How do I view all the files (including those in subdirectories) ordered by last modified date?

From Dev

How to recursively remove all empty folders in PowerShell?

From Dev

How to get all class properties (in Python) of a GAE polymodel including those defined via @property

From Dev

How to delete all the files in a folder including the files in the sub folders without deleting the the folder itself or any of its sub folders

From Dev

Recursively do something with all files in a given directory including subdirectory files

From Dev

How to use grep on all files non-recursively in a directory?

From Dev

How to use grep on all files non-recursively in a directory?

From Dev

Recursively import all .py files from all folders

From Dev

rsync all files recursively - exclude all folders (but not their contents)

From Dev

Accessing files inside folders in a zipfile

From Dev

How to use bash to print all items in a list, including those delimited by commas?

From Java

How to revert uncommitted changes including files and folders?

From Dev

How to diff all modifications of all checkout files under a directory recursively with Clearcase?

From Dev

How to diff all modifications of all checkout files under a directory recursively with Clearcase?

From Dev

Recursively set all files and folders to 777 from this list

From Dev

Recursively deleting all files and folders from a given FTP path

Related Related

  1. 1

    How to use ZipFile Class in Java to recursively open all files including those under Folders

  2. 2

    (Batch) How to recursively delete all files/folders in a directory except those with a leading .?

  3. 3

    How to display all files under folders in treeview

  4. 4

    Command to remove all files and folders recursively including, hidden ones, on MAC OS Terminal?

  5. 5

    How can we list all files and folders recursively?

  6. 6

    recursively delete all files, empty directories, and directories with files of multiple names under current directory, including current directory

  7. 7

    How to use Terminal to delete all .svn folders recursively?

  8. 8

    zip all files and folders recursively in bash

  9. 9

    How to recursively delete files and folders under a certain directory using command line?

  10. 10

    How to recursively search a directory for all files including hidden files in hidden directories, with PowerShell?

  11. 11

    How to rename folders and files recursively in MacOS

  12. 12

    How to find all empty files and folders in a specific directory including files which just look empty but are not?

  13. 13

    How to find all empty files and folders in a specific directory including files which just look empty but are not?

  14. 14

    How do I view all the files (including those in subdirectories) ordered by last modified date?

  15. 15

    How to recursively remove all empty folders in PowerShell?

  16. 16

    How to get all class properties (in Python) of a GAE polymodel including those defined via @property

  17. 17

    How to delete all the files in a folder including the files in the sub folders without deleting the the folder itself or any of its sub folders

  18. 18

    Recursively do something with all files in a given directory including subdirectory files

  19. 19

    How to use grep on all files non-recursively in a directory?

  20. 20

    How to use grep on all files non-recursively in a directory?

  21. 21

    Recursively import all .py files from all folders

  22. 22

    rsync all files recursively - exclude all folders (but not their contents)

  23. 23

    Accessing files inside folders in a zipfile

  24. 24

    How to use bash to print all items in a list, including those delimited by commas?

  25. 25

    How to revert uncommitted changes including files and folders?

  26. 26

    How to diff all modifications of all checkout files under a directory recursively with Clearcase?

  27. 27

    How to diff all modifications of all checkout files under a directory recursively with Clearcase?

  28. 28

    Recursively set all files and folders to 777 from this list

  29. 29

    Recursively deleting all files and folders from a given FTP path

HotTag

Archive