使用Zip4j在Android中创建zipFile时出现异常:可能不是zip文件或损坏的zip文件

时间:2016-07-19 10:04:51

标签: java android exception zip zip4j

我在使用zip4j库在android中生成zipfile时遇到问题,这里是代码:

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();
}

我得到了异常消息:

    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)

此代码在Eclipse中适用于本地文件,但在Android中效果不佳,我很确定目标.zip路径是正确的,zip4j库版本是1.3.2。

任何人都可以给我任何建议吗?

2 个答案:

答案 0 :(得分:1)

我遇到了非常相似的堆栈跟踪。但是,我在桌面上使用版本1.3.2中的java和zip4j。我不确定我的答案与android有什么关系,但在这里它会发生。

我的堆栈跟踪

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)

我像这样使用zip4j

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

要解决问题我改为

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

不同的是Files.createTempFile()创建空文件,文件必须搞砸ZipFile的构造函数。当我以未占用的路径通过File时,问题就消失了。

答案 1 :(得分:0)

我遇到了同样的问题,因为我使用了AES加密,但没有设置参数AES数据密钥。

enter image description here