0

I need to download gzipped data chunkwise and append it to file. The problem is that when i read from the HTTP conn, it is not sending total compressed byte array at a single time(stream). my Application is looking for Gzip header in the left over bytes of the byte array sent before. how can i force the http to send the compressed instance of the byte array all at once. Here's my code snippet that takes the data compresses it and adds it to a file.

if (responseCode == HttpConnection.HTTP_OK)
{
    boolean stop = false, pause = false;
    totalSize = conn.getLength() + downloaded;
    chunkSize = (int)(conn.getLength() / 100);
    System.out.println("*********-----" + conn.getLength() + "");
    System.out.println("-----------------ok");
    in = conn.openInputStream();
    int length = 0, s = 0;
    byte[] readBlock = new byte[(int)conn.getLength()];

    while ((s = in.read(readBlock) != -1)
            length = length + s;
    {
          if (!pause)
            {
                readBlock = Decompress.decompress(readBlock);
                out.write(readBlock, 0, length);
                downloaded += length;
                int a = getPerComplete(totalSize, downloaded);
                System.out.println("% OF Downloaded--------" + a);
                int a1 = getPerComplete(totalSize, downloaded);

Decompress function:-

    public byte[] decompress(byte[] compressed) throws IOException
    {
        GZIPInputStream gzipInputStream;
        if (compressed.length > 4)
        {
            gzipInputStream = new GZIPInputStream(
                new ByteArrayInputStream(compressed, 4,
                                         compressed.length - 4));

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            for (int value = 0; value != -1;)
            {
                value = gzipInputStream.read();
                if (value != -1)
                {
                    baos.write(value);
                }
            }
            gzipInputStream.close();
            baos.close();

            return baos.toByteArray();
        }
        else
        {
           return null;
        }
    }
}
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
shashankkb
  • 23
  • 6
  • 1
    Short answer: you can't, you will have to read the byte array in a while loop untill all is read. – rsp Dec 18 '12 at 08:51
  • @rsp : i would have done that but i don't know to what size the data compresses to.. so how do i dynamically read compressed data size – shashankkb Dec 18 '12 at 09:22
  • When the read returns `-1` you have reached the end of the zip entry. Btw, using `read(byte[] b, int off, int len)` is much more efficient than reading 1 byte at a time. – rsp Dec 18 '12 at 10:16
  • Thanks. I got how to do it... – shashankkb Dec 18 '12 at 11:48

0 Answers0