簡體   English   中英

java.util.zip.ZipException:ZIP文件中的條目太多

[英]java.util.zip.ZipException: too many entries in ZIP file

我正在嘗試編寫一個Java類來提取包含~74000個XML文件的大型zip文件。 嘗試使用java zip庫解壓縮時,我得到以下異常:

java.util.zip.ZipException :ZIP文件中的條目太多

不幸的是,由於項目的要求,我無法在它到達之前將拉鏈打破,並且解壓縮過程必須自動化(無需手動步驟)。 有沒有辦法利用java.util.zip或某些第三方Java zip庫解決這個限制?

謝謝。

使用ZipInputStream而不是ZipFile可能應該這樣做。

使用apache IOUtils:

FileInputStream fin = new FileInputStream(zip);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;

while ((ze = zin.getNextEntry()) != null) {
    FileOutputStream fout = new FileOutputStream(new File(
                    outputDirectory, ze.getName()));

    IOUtils.copy(zin, fout);

    IOUtils.closeQuietly(fout);
    zin.closeEntry();
}

IOUtils.closeQuietly(zin);

Zip標准在文件中最多支持65536個條目。 除非Java庫支持ZIP64擴展,否則如果您嘗試讀取或寫入包含74,000個條目的存檔,它將無法正常工作。

我重新設計了處理目錄結構更方便的方法,並一次壓縮了一大堆目標。 普通文件將添加到zip文件的根目錄中,如果傳遞目錄,則將保留基礎結構。

def zip (String zipFile, String [] filesToZip){ 
    def result = new ZipOutputStream(new FileOutputStream(zipFile))
    result.withStream { zipOutStream ->
        filesToZip.each {fileToZip ->
            ftz = new File(fileToZip)
            if(ftz.isDirectory()){
                pathlength = new File(ftz.absolutePath).parentFile.absolutePath.size()
                ftz.eachFileRecurse {f ->               
                    if(!f.isDirectory()) writeZipEntry(f, zipOutStream, f.absolutePath[pathlength..-1]) 
                }
            }               
            else writeZipEntry(ftz, zipOutStream, '')
        }
    }
}

def writeZipEntry(File plainFile, ZipOutputStream zipOutStream, String path) {
    zipOutStream.putNextEntry(new ZipEntry(path+plainFile.name))
    new FileInputStream(plainFile).withStream { inStream ->
        def buffer = new byte[1024]
        def count
        while((count = inStream.read(buffer, 0, 1024)) != -1) 
            zipOutStream.write(buffer)                  
    }
    zipOutStream.closeEntry()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM