Exception when create zipFile in Android with Zip4j: Probably not a zip file or a corrupted zip file

你说的曾经没有我的故事 提交于 2020-08-05 07:25:10

问题


I meet a problem when using zip4j library to generate zipfile in android, here's the code :

try {
    ZipFile zipFile = new ZipFile(dest);
    zipFile.setFileNameCharset("GBK");
    if (srcFile.isDirectory()) {
        zipFile.addFolder(srcFile, parameters);
    } else {
        zipFile.addFile(srcFile, parameters);
    }
} catch (Exception e) {
    e.printStackTrace();
}

and I got the Exception msg:

    net.lingala.zip4j.exception.ZipException: Probably not a zip file or a corrupted zip file
    at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:179)
    at net.lingala.zip4j.core.HeaderReader.readAllHeaders(HeaderReader.java:78)
    at net.lingala.zip4j.core.ZipFile.readZipInfo(ZipFile.java:425)
    at net.lingala.zip4j.core.ZipFile.checkZipModel(ZipFile.java:935)
    at net.lingala.zip4j.core.ZipFile.addFiles(ZipFile.java:263)
    at net.lingala.zip4j.core.ZipFile.addFile(ZipFile.java:250)
Caused by: java.io.IOException: Negative seek offset
    at java.io.RandomAccessFile.seek(RandomAccessFile.java:555)
    at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:117)

This code works perfect in Eclipse for local files, but does not work well in Android, I'm pretty sure the destination .zip path is correct, and the zip4j library version is 1.3.2.

could anyone give me any suggestion?


回答1:


I came across very similar stacktrace. However, I use java and zip4j in version 1.3.2 on desktop. I am not sure how my answer is relevant to android but here it goes.

My stacktrace

Caused by: net.lingala.zip4j.exception.ZipException: Probably not a zip file or a corrupted zip file
        at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:179)
        at net.lingala.zip4j.core.HeaderReader.readAllHeaders(HeaderReader.java:78)
        at net.lingala.zip4j.core.ZipFile.readZipInfo(ZipFile.java:425)
        at net.lingala.zip4j.core.ZipFile.checkZipModel(ZipFile.java:935)
        at net.lingala.zip4j.core.ZipFile.addFolder(ZipFile.java:343)
        at net.lingala.zip4j.core.ZipFile.addFolder(ZipFile.java:330)
Caused by: java.io.IOException: Negative seek offset
        at java.io.RandomAccessFile.seek(RandomAccessFile.java:555)
        at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:117)
        at net.lingala.zip4j.core.HeaderReader.readAllHeaders(HeaderReader.java:78)

I use zip4j like this

Path destination = Files.createTempFile("export", ".zip");
ZipFile zip = new ZipFile(destination.toFile());

To fix the problem I changed to

File destination = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
ZipFile zip = new ZipFile(destination);

The difference is Files.createTempFile() creates empty file and the file must screw up ZipFile's constructor. When I passed File with unoccupied path, the problem was gone.




回答2:


I met with the same question because of I used AES encryption but didn't set the parameters AES data key.



来源:https://stackoverflow.com/questions/38455387/exception-when-create-zipfile-in-android-with-zip4j-probably-not-a-zip-file-or

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