2

我正在尝试将 zip 文件下载到 SD 卡。我正确下载了它,但是当我打开下载的文件(使用 ZipFile)时,我得到了这个 ZipException(“找不到中央目录条目”)。

Internet-file 没问题,SD-copy-file 没问题(从 PC 打开并正确显示文件),但由于某种原因在 Android 中不起作用。

下载代码:

BufferedInputStream stream = null;
try {
  stream = new BufferedInputStream(is, 8192);
}
....

try { ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = stream.read()) != -1 ) baf.append((byte) current);

BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(path)); fos.write(baf.toByteArray()); fos.close(); } ...

我认为问题出在 ZIP 文件头中,它没有正确写入,但我不知道是什么原因。源代码 ZipEntry 类向我展示了这一点:

long sig = (hdrBuf[0] & 0xff) | ((hdrBuf[1] & 0xff) < <  8) |
           ((hdrBuf[2] & 0xff) < <  16) | ((hdrBuf[3] < <  24) & 0xffffffffL);
if (sig != CENSIG) {
     throw new ZipException("Central Directory Entry not found");
}

谢谢,

4

1 回答 1

3

自动回答:问题在于 HTTP 请求使用Accept-Encoding: gzip

服务器返回一个已经压缩的文件并下载它,解压缩它,删除部分标题。

不幸的是,7zip 正确打开(可能,不检查标题),但 Android 没有打开文件(可能,检查标题)。

简而言之:小心,并正确检查某些文件的文件编码。

于 2010-12-14T22:58:48.557 回答