简体   繁体   中英

How to read all “.java” files from a “.zip” file

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

Here is the code that can read and print the .java files in zip files::

You can also get the file and its content using this code.

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;

public class test {
    public static void main(String[] args) throws IOException {
        ZipFile zipFile = new ZipFile("test.zip");
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        System.out.println(entries);

        while(entries.hasMoreElements()){
            ZipEntry entry = entries.nextElement();
            System.out.println(entry.getName());
            if(entry.getName().contains("java")){
                InputStream stream = zipFile.getInputStream(entry);
                System.out.println(IOUtils.toString(stream, StandardCharsets.UTF_8));
            }
        }
    }
}

Maybe you can directly use File like below:

File javaFile = new File(zipEntry.getName());
javaFiles.add(javaFile); 

Please check with this code

 ZipFile zipFile = new ZipFile("C:/test.zip");

Enumeration<? extends ZipEntry> entries = zipFile.entries();

while(entries.hasMoreElements()){
    ZipEntry entry = entries.nextElement();
    InputStream stream = zipFile.getInputStream(entry);
}

If you want a list of Files that you will later traverse, then first you need to extract the files somewhere. The others answers have examples of how to fetch the inputstream and then write it to disk so I won't repeat that.

Once you extract the files, then you can populate your list with references to them.

ps I am guessing that you are doing this for a project or something but in general tools like Sonarqube are great from static code analysis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM