我需要chunkwise下载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;
}
}
}