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

时间:2016-11-03 10:42:03

标签: zip java-7 unzip zip4j

我开发了一个实用程序来将图像添加到现有的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 lib。

请帮助解决此问题。

1 个答案:

答案 0 :(得分:0)

感谢所有人,

我解决了这个问题。我使用Apache tika来处理文件类型,而不是将新文件添加到zip文件中。

再次感谢所有人。