未找到中央目录条目 (ZipException)
我正在尝试将 zip 文件下载到 SD 卡。我正确下载了它,但是当我打开下载的文件(使用 ZipFile)时,我收到此 ZipException(“未找到中央目录条目”)。
互联网文件没问题,SD 复制文件没问题(从 PC 打开并正确显示文件),但由于某种原因在 Android 中不起作用。
下载代码:
BufferedInputStream stream = null;
try {
stream = new BufferedInputStream(is, 8192);
}
....
尝试 {
ByteArrayBuffer baf = 新的 ByteArrayBuffer(50);
整数当前= 0;
while ((当前 = Stream.read()) != -1 )
baf.append((字节)当前);
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");
}
谢谢,
I am trying download zip file to SD card. I download it correctly, but when I open downloaded file (with ZipFile) I get this ZipException ("Central Directory Entry not found").
Internet-file is okay, SD-copy-file is okay (from PC opened and show files correctly), but for some reason don't work in Android.
Code for download:
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(); } ...
I supossed that the problem is in the ZIP file headers, which was not properly written, but I do not know for what reason. The source code ZipEntry class shows me this:
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");
}
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自动应答:问题是 HTTP 请求使用 Accept-Encoding: gzip。
服务器返回一个已经压缩的文件并下载它,解压缩它,删除部分标头。
不幸的是,7zip 正确打开(可能没有检查标头),但 Android 无法打开文件(可能没有检查标头)。
简而言之:小心,并检查某些文件的文件编码是否正确。
Auto-answer: The problem was that HTTP request use Accept-Encoding: gzip.
The server returned an already compressed file and download it, decompress it, removing part of the header.
Unfortunately, 7zip opened correctly (probably, not check headers), but Android not open file (probably, check headers).
In short: Be careful, and check correctly file-encoding with some files.