2017-10-17 351 views
1
FileInputStream file = new FileInputStream("/file/path/report.xlsx"); 
XSSFWorkbook wb = new XSSFWorkbook(file); --!ERROR LINE!-- 

我在第二行發生錯誤。我看着不同的網上資源,比如這個:java.io.IOException:無法讀取zip條目源

Error reading Excel .XLSX with Apache POI

Exception in thread "main" java.io.IOException: Failed to read zip entry source 
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:106) 
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:342) 
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:285) 
    at test.test.main(test.java:48) 
Caused by: java.io.IOException: Zip bomb detected! The file would exceed the max. ratio of compressed file size to the size of the expanded data. This may indicate that the file is used to inflate memory usage and thus could pose a security risk. You can adjust this limit via ZipSecureFile.setMinInflateRatio() if you need to work with files which exceed this limit. Counter: 1483367, cis.counter: 14796, ratio: 0.009974605070761314Limits: MIN_INFLATE_RATIO: 0.01 
    at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.advance(ZipSecureFile.java:257) 
    at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:214) 
    at java.io.FilterInputStream.read(Unknown Source) 
    at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:132) 
    at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:56) 
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:99) 
    ... 4 more 

我加入ZipSecureFile.setMinInflateRatio(0.009);

FileInputStream file = new FileInputStream("/file/path/report.xlsx"); 
    ZipSecureFile.setMinInflateRatio(0.009); 
    XSSFWorkbook wb = new XSSFWorkbook(file); 

它能正常工作了一段時間,然後它開始拋出了同樣的錯誤

任何幫助或指導表示讚賞。

+0

如果您信任的文件,如果你把在更晚的比例會發生什麼? – Gagravarr

+0

@Gagravarr我相信該文件,有沒有辦法來覆蓋檢查?像ZipSecureFile.setMinInflateRatio(-1.0d); ? – Chid

+0

IIRC您可以設置'0'的比率來禁用檢查,但只有在您信任該文件時纔會這樣做! – Gagravarr

回答

2

添加下面的行對我來說是個訣竅。

ZipSecureFile.setMinInflateRatio(-1.0d); 

代碼如下:

FileInputStream file = new FileInputStream("/file/path/report.xlsx"); 
ZipSecureFile.setMinInflateRatio(-1.0d); 
XSSFWorkbook wb = new XSSFWorkbook(file); 
相關問題