javaandroidexceptionzipzip4j

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


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?


Solution

  • 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.