0

I have the following, which fails to print a string to console as I would expect it to:

    String url = "http://mis.ercot.com/misdownload/servlets/mirDownload? 
                                        mimic_duns=000000000&doclookupId=698819309";
        URL obj = new URL(url.trim());
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.getResponseCode();
        InputStream stream = conn.getInputStream();
        ZipInputStream zis = new ZipInputStream(stream);
        byte[] buffer = new byte[1024];
        int i = zis.read(buffer);
        String str = new String(buffer, StandardCharsets.UTF_8);
        System.out.println(str);

At the URL, there is zip file, which has an XML file which I need to eventually parse, but for now I will be satisfied to simply print it to the console, and thus confirm I have succesfully unzipped the file.

Something like the following does indeed generate the correct name of the file:

        ZipEntry ze = zis.getNextEntry();
        System.out.println(ze);

Thus I know I am on the right track, but I do not need the name of the file, I do not even need the file, I need the XML entries, because my eventual goal is to persist to SQL.

Devin Andres Salemi
  • 2,198
  • 3
  • 12
  • 25
  • You need to read the Javadoc. This is not a correct way to use a `ZipInputStream`. You need to iterate over the entries. – user207421 Feb 02 '20 at 02:11

1 Answers1

-1

The following loop should get you started

ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
    byte contents[] = new byte[4096];
    int direct;
    while ((direct = zis.read(contents, 0, contents.length)) >= 0) {
        System.out.println(entry.getName() + ", " + direct + " bytes");
        for (byte con : contents) {
            System.out.print((char) con);
        }
    }
    System.out.println();
}

Adapted from How to read file from ZIP using InputStream?

JavaMan
  • 1,142
  • 12
  • 22