3

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;
}
Papershine
  • 4,995
  • 2
  • 24
  • 48
  • How do you want to use returned `List`? What is its purpose? – Pshemo Sep 14 '17 at 11:32
  • Please check this way [link](https://stackoverflow.com/a/36890475/1012497) – nikli Sep 14 '17 at 11:33
  • I want to get all the comments and identifiers of the java files to check code smells. So I wanted to get all the java files from this class. Then work with those files (i.e get comments and identifiers to check code smells). @Pshemo – Sharafat Ahmed Sabir Sep 14 '17 at 11:37
  • 1
    `File` describes location of file like zip file, not its entry. Maybe you want URI instead. Visit "[Get a File or URI object for a file inside an archive with Java?](https://stackoverflow.com/q/2049659)" and "[Read directly a file within a Zip file - Java](https://stackoverflow.com/q/13399448)" for more info. – Pshemo Sep 14 '17 at 11:54
  • Thanks for the clarifications and the links. This helps a lot. @Pshemo – Sharafat Ahmed Sabir Sep 14 '17 at 12:15

4 Answers4

6

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));
            }
        }
    }
}
Abhishek Honey
  • 645
  • 4
  • 13
0

Maybe you can directly use File like below:

File javaFile = new File(zipEntry.getName());
javaFiles.add(javaFile); 
nikli
  • 2,281
  • 2
  • 24
  • 38
0

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);
}
Vimesh c
  • 41
  • 2
  • 10
0

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.

p.s. 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.

ssc327
  • 690
  • 1
  • 9
  • 19