How to read all ".java" files from a ".zip" file

Viewed 3452

I want to get all .java files and put them in an ArrayList of files. I have taken a ZipInputStream and then a ZipEntry to iterate through files but can not understand how to get the files from the ZipEntry.

public List<File> getJavaFiles(MultipartFile file){
    List<File> javaFiles = new ArrayList<File>();

    ZipEntry zipEntry;

    log.info("getJavaFiles");
    try {
        ZipInputStream zip;
        try {
            zip = new ZipInputStream( file.getInputStream());
            while((zipEntry = zip.getNextEntry()) != null){
                if(zipEntry.getName().endsWith(".java")){
                    log.info(zipEntry.getName());

                    //How do I put the java file in my array list
                }
            }
            zip.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return javaFiles;
}
4 Answers
Related