1

I am testing a framework in which I download zip file trough HTTP Get. I use Apache HttpClient for that. I am having hard time saving a response that is a zip file. I tried just Output Stream pointing to the path that end with ".zip" but it is not working, the zip is invalid. I tried to ZipOuputStream but then it asks for entry, and I don't know the entry. I am trying unzip it as it comes to get the entry then and so far, nothing is working. Here are some variations I've tried so far

**           ZipInputStream zipStream = new ZipInputStream(response.getEntity().getContent());
        ZipEntry entry = null;
        Scanner sc = new Scanner(zipStream);

        while ((entry = zipStream.getNextEntry()) != null) {

            String entryName = entry.getName();
            System.out.println(entryName);

            FileOutputStream out = new FileOutputStream(entryName);

            byte[] byteBuff = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = zipStream.read(byteBuff)) != -1)
            {
                out.write(byteBuff, 0, bytesRead);
            }

            out.close();
            zipStream.closeEntry();
        }
        zipStream.close();

**

The above code doesn't see the zipStream.getNextEntry(), it is always null, with or without the Scanner it is same result, Scanner also always returns null.

Or same thing but the beginning being byte array

            byte[] bytes = EntityUtils.toByteArray(response.getEntity());
        ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes));

Same issue as the above, when I later start processing it with while ((entry = zipStream.getNextEntry()) != null) to get files from it, it is also always null for the next element

or

           byte[] bytes = EntityUtils.toByteArray(response.getEntity());
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(filePath));
        zos.write(bytes);

In this case there is error that the file requires ZipEntry and I don't know how to obtain it. I've tried with the above code for unziping but the element is always.

            FileOutputStream fos = new FileOutputStream(filePath);
        response.getEntity().writeTo(fos);

This writes a file but is invalid, I can't open it manually coz it is always giving me an error.

            FileOutputStream fos = new FileOutputStream(filePath);
        ZipOutputStream zos = new ZipOutputStream(fos);
        response.getEntity().writeTo(zos);

This is throwing java.util.zip.ZipException: no current ZIP entry so zip I can open zip in Windows but is empty since it breaks.

But nothing is working so far

EDIT I am not sure what is it relating to but the corrupted file that I am getting through some of the approaches is 757kB while the one I get through swagger is 14,726kB. Is there a limitation on the data transfer for entity?

Ver Siw
  • 11
  • 2
  • What does "nothing is working so far" mean? What behavior do you get for each of the versions of your code you present? Why are you creating an instance of `Scanner`? That could be messing you up, and you aren't using it anyway. I'd get rid of that line. Other than that, your code looks ok without me trying to compile or run it. Knowing what error you're getting, including a full stack trace, would be helpful. – CryptoFool Oct 19 '22 at 23:35
  • @CryptoFool I edited the question with behavior for each code variation I tried. Scanner line is irrelevant, I was grasping for straws to see if I can somehow see next element / line – Ver Siw Oct 20 '22 at 15:36
  • So, you (say) are *very sure*, that `response.getEntity().getContent()` is an "input stream" with "valid zip"!?? Please convince us as well! (I *strongly* doubt) – xerx593 Oct 20 '22 at 15:45
  • @xerx593 I can get it if I use swagger interface. There is a python code which I assume works but never tried it myself. – Ver Siw Oct 20 '22 at 16:27

1 Answers1

0

So, I did mess up the request. I was requesting json only. All the responses were on point with the suggestion that return from the call is not a proper zip file. And majority of the options above should work with the proper zip incoming. Thanks for all the help

Ver Siw
  • 11
  • 2