问题
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