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 )
开发者_StackOverflow社区 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,
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.
精彩评论