How to unzip a file from InputStream

I am trying to get a zip file from the server. I am using HttpURLConnection to get an InputStream, and this is what I have:

myInputStream.toString().getBytes().toString() is equal to [ B@4..... byte[] bytes = Base64.decode(myInputStream.toString(), Base64.DEFAULT); String string = new String(bytes, "UTF-8"); string ==  &Ü¢  z m    y.... 

I really tried to unzip this file, but nothing works, there are also so many questions, should I use GZIPInputStream or ZipInputStream? I need to save this stream as a file, or I can work with InputStream

Please help, my boss becomes impatient: O I do not know what I have to find out in this file :)

+6
source share
2 answers

basically, if the inputStream parameter is GZIPInputStream , it should be able to read the actual content. Also for ease of use, the IOUtils package from apache.commons simplifies life

this works for me:

  InputStream is ; //initialize you IS is = new GZIPInputStream(is); byte[] bytes = IOUtils.toByteArray(is); String s = new String(bytes); System.out.println(s); 
-1
source

Usually there is no significant difference between GZIPInputStream or ZipInputStream, so, if at all, both should work.

Next, you need to determine if the encrypted stream was Base64 encoded, or if some Base64 encoded content was put into the archived stream - from what you put to your question, this is apparently the last option.

So you should try

 ZipInputStream zis = new ZipInputStream( myInputStream ); ZipEntry ze = zis.getNextEntry(); InputStream is = zis.getInputStream( ze ); 

and proceed from there ...

-1
source

All Articles