Mic
Mic

Reputation: 23

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?

Upvotes: 2

Views: 6865

Answers (3)

Mudit Goel
Mudit Goel

Reputation: 354

The working code for this along with the password protected zip is :

File dbFile = new File(ctx.getExternalFilesDir("") + File.separator +
                        AppConstants.DB_PATH + File.separator + AppConstants.DB_NAME);
zipName = AppConstants.DB_NAME + "-" + getCurrentDateTime() + ".zip";
ZipFile zipFile = new ZipFile(folderPath + File.separator + zipName);

ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(AppUtil.decodeStr(AppConstants.DB_PASSWORD));
zipFile.addFile(dbFile, parameters);

However the issue may still appear because of the following reasons:

  • Either the source file does not exists while adding to zipFile,
  • The app do not have permissions to read the source file,
  • The target zip file might consist of special symbols or space in the file name.

Upvotes: 0

xiaotianrou
xiaotianrou

Reputation: 9

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

enter image description here

Upvotes: -1

lord.didger
lord.didger

Reputation: 1407

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.

Upvotes: 7

Related Questions