Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 分块压缩数据的解压缩_Java_Blackberry_Httpconnection_Gzipinputstream - Fatal编程技术网

Java 分块压缩数据的解压缩

Java 分块压缩数据的解压缩,java,blackberry,httpconnection,gzipinputstream,Java,Blackberry,Httpconnection,Gzipinputstream,我需要分块下载Gzip数据并将其附加到文件中。问题是,当我从HTTP conn读取时,它并不是一次性发送总的压缩字节数组(流)。我的应用程序在之前发送的字节数组的剩余字节中查找Gzip头。如何强制http一次性发送字节数组的压缩实例。 下面是我的代码片段,它将数据压缩并添加到文件中 if (responseCode == HttpConnection.HTTP_OK) { boolean stop = false, pause = false; totalSize = conn.

我需要分块下载Gzip数据并将其附加到文件中。问题是,当我从HTTP conn读取时,它并不是一次性发送总的压缩字节数组(流)。我的应用程序在之前发送的字节数组的剩余字节中查找Gzip头。如何强制http一次性发送字节数组的压缩实例。 下面是我的代码片段,它将数据压缩并添加到文件中

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);
解压功能:-

    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;
        }
    }
}

简短回答:不行,您必须在while循环中读取字节数组,直到所有内容都被读取。@rsp:我本来可以这样做,但我不知道数据压缩到什么大小。。因此,如何动态读取压缩数据大小当读取返回
-1
时,您已到达zip条目的末尾。顺便说一句,使用
读取(byte[]b,int off,int len)
比一次读取一个字节要高效得多。谢谢。我知道怎么做了。。。