How to unzip file from InputStream
Asked Answered
S

3

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:)

Stardom answered 19/8, 2015 at 10:22 Comment(3)
Check this for downloadingTemptation
write this zip file to sdcard and then try to unzip it. Don't Forget to Give Read and Write PermissionVolteface
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.Rapscallion
T
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.

Talion answered 27/11, 2019 at 12:6 Comment(0)
D
-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);
Drobman answered 19/8, 2015 at 10:33 Comment(2)
While this code may solve the OP's problem, a few words of explanation would be even more helpful to future readers.Lory
@TheThom yeah ur right mate, sorry I was abit busy at the time of the post. I've eddited my post.Drobman
S
-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 ...

Skinnydip answered 19/8, 2015 at 10:40 Comment(2)
Rubbish. Once is a stream compression method: the other is a system for compression a file set.Ambient
@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?Skinnydip

© 2022 - 2024 — McMap. All rights reserved.