Central Directory Entry not found (ZipException)

吃可爱长大的小学妹 提交于 2019-12-02 04:55:46

问题


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,


回答1:


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.



来源:https://stackoverflow.com/questions/4408841/central-directory-entry-not-found-zipexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!