0

我开发了一个实用程序来将图像添加到现有的 zip 文件中。在大多数情况下,我得到了肯定的结果。但在同样的情况下,实用程序抛出错误

未找到 zip 标头。可能不是 zip 文件

但文件在“归档管理器”或 winrar 中打开。我的代码如下:

public static boolean addFileIntoZip(String sourceFile, String zipFile) {
        try {
            logger.debug("add file in Zip>>>>>>>>>>");
            logger.debug("Source Folder {} and Destination Folder {}", sourceFile, zipFile);
            ZipFile file = new ZipFile(zipFile);
            File sFile = new File(sourceFile);
            // Initiate Zip Parameters which define various properties such
            // as compression method, etc.
            ZipParameters parameters = new ZipParameters();

            // set compression method to store compression
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);

            // Set the compression level
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

            file.addFile(sFile, parameters);

            return true;
        } catch (ZipException e) {
            e.printStackTrace();
        }
        return false;
    }

错误堆栈是

2016-11-03 15:55:39 [DEBUG] (ZipUtil.java:121) => add file in Zip>>>>>>>>>>
2016-11-03 15:55:39 [DEBUG] (ZipUtil.java:122) => Source Folder /home/sanjeet/Downloads/DocumentViewer_architecture.png and Destination Folder /home/sanjeet/Downloads/tst/000033541066253_D.zip
net.lingala.zip4j.exception.ZipException: zip headers not found. probably not a zip file
    at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:122)
    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)
    at com.grit.docs.util.ZipUtil.addFileIntoZip(ZipUtil.java:135)
    at com.grit.docs.util.ZipUtil.main(ZipUtil.java:160)

文件也不能通过 ubuntu 中的解压缩工具解压缩。它显示错误为

Archive:  000033541066253_D.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of 000033541066253_D.zip or
        000033541066253_D.zip.zip, and cannot find 000033541066253_D.zip.ZIP, period.

我使用 zip4j zip 库。

请帮助解决这个问题。

4

1 回答 1

0

谢谢大家,

我解决了这个问题。我使用 Apache tika 来优化文件类型,而不是向 zip 文件添加新文件。

再次感谢大家。

于 2017-02-02T08:59:29.350 回答