8

I'm trying to get a zip file from the server. Im using HttpURLConnection to get 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 realy tried to unzip this file but nothing works, also there is so many questions, should I use GZIPInputStream or ZipInputStream? Do I have to save this stream as file, or I can work on InputStream

Please help, my boss is getting impatient:O I have no idea what is in this file i have to find out:)

Radek.T
  • 95
  • 2
  • 8
  • 1
    Check [this](http://stackoverflow.com/a/3223510/916225) for downloading – seenukarthi Aug 19 '15 at 10:25
  • write this zip file to sdcard and then try to unzip it. Don't Forget to Give Read and Write Permission – AmniX Aug 19 '15 at 10:28
  • The use of `inputStream.toString()` certainly does not do what you think it does. Just as `java.io.File.toString()` does not print the **contents** of a file as a String, but something else (the path of the file), which is useless if you are interested in the content. – GPI Aug 19 '15 at 10:59

3 Answers3

0

GZipInputStream and ZipInputStream are two different formats. https://en.wikipedia.org/wiki/Gzip

It is not a good idea to retrieve a string directly from the stream.From an InputStream, you can create a File and write data into it using a FileOutputStream.

Decoding in Base 64 is something else. If your stream has already decoded the format upstream, it's OK; otherwise you have to encapsulate your stream with another input stream that decodes the Base64 format. The best practice is to use a buffer to avoid memory overflow.

Here is some Kotlin code that decompresses the InputStream zipped into a file. (simpler than java because the management of byte [] is tedious) :

val fileBinaryDecompress = File(..path..)
val outputStream = FileOutputStream(fileBinaryDecompress)

readFromStream(ZipInputStream(myInputStream), BUFFER_SIZE_BYTES,
    object : ReadBytes {
         override fun read(buffer: ByteArray) {
             outputStream.write(buffer)
         }
    })
outputStream.close()

interface ReadBytes {
    /**
     * Called after each buffer fill
     * @param buffer filled
     */
    @Throws(IOException::class)
    fun read(buffer: ByteArray)
}

@Throws(IOException::class)
fun readFromStream(inputStream: InputStream, bufferSize: Int, readBytes: ReadBytes) {
    val buffer = ByteArray(bufferSize)
    var read = 0
    while (read != -1) {
        read = inputStream.read(buffer, 0, buffer.size)
        if (read != -1) {
            val optimizedBuffer: ByteArray = if (buffer.size == read) {
                buffer
            } else {
                buffer.copyOf(read)
            }
            readBytes.read(optimizedBuffer)
        }
    }
}

If you want to get the file from the server without decompressing it, remove the ZipInputStream() decorator.

J-Jamet
  • 847
  • 8
  • 17
-1

basically by setting inputStream to be GZIPInputStream should be able to read the actual content. Also for simplicity using IOUtils package from apache.commons makes your life easy

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);
nafas
  • 5,283
  • 3
  • 29
  • 57
  • 1
    While this code may solve the OP's problem, a few words of explanation would be even more helpful to future readers. – Thom Aug 19 '15 at 12:02
  • 1
    @TheThom yeah ur right mate, sorry I was abit busy at the time of the post. I've eddited my post. – nafas Aug 19 '15 at 12:28
-1

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

Next, you need to identify whether the zipped stream was Base64 encoded, or the some Base64 encoded contents was put into a zipped stream - from what you put to your question, it seems to be the latter option.

So you should try

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

and proceed from there ...

tquadrat
  • 3,033
  • 1
  • 16
  • 29
  • Rubbish. Once is a stream compression method: the other is a system for compression a file set. – user207421 Aug 19 '15 at 11:01
  • @EJP: Even if declaring something as rubbish, correct wording would be appreciated: ONE is a stream compression method (GZIPInputStream), the other is a method for COMPRESSING a file set (ZipInputStream). And some more details why the answer should be rubbish would be nice, too - and when you know better, why haven't you answered the question? – tquadrat Aug 20 '15 at 11:44