0

I'm trying to make a function that takes in a zip file name as a String and prints everything in it. I have made it so that i need the actual zip file but I do not know how to turn the String into a zip file.

public void zipInput(ZipFile zipFile) { 
    
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        try {
            InputStream is = zipFile.getInputStream(e);
            System.out.println(is);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

}
Progman
  • 16,827
  • 6
  • 33
  • 48
  • 2
    Have you tried `new ZipFile(yourString);`? See https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html#ZipFile(java.lang.String) – Progman Jul 06 '21 at 18:55
  • printing `InputStream` is not a good idea. In order to see "content" you first needs to consume it. So in the end, this question is [duplicate](https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java) – rkosegi Jul 06 '21 at 18:57

1 Answers1

2

A zip file can be constructed with a File. So you can do:

File file = new File(<your path name as a string>);
ZipFile zip = new ZipFile (file);

An for the ZipEntry, there is a getName() method. So you can just do:

public void zipInput(ZipFile zipFile) { 
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        try {
            System.out.println(e.getName();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
Hervé Girod
  • 465
  • 3
  • 12