我想解压缩使用Windows10的压缩功能创建的.zip文件(其中包含.jpg文件)。
首先,我用Java 8的本机util.zip.ZipEntry
测试了它,但一直得到一个invalid CEN header (bad compression method)
错误,这似乎是由于与Win10 10的压缩不兼容造成的。
正因为如此,我切换到ApacheCommon1.2版本的Compress
库。存档中的前两个图像解压缩很好,但是第三个总是抛出异常:
org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException:不支持压缩方法14 (LZMA)用于输入图像3.jpg
如何使用Compress
库完全解压缩这个归档?这可能吗?
我的代码:
ZipFile z = new ZipFile(zippath);
Enumeration<ZipArchiveEntry> entries = z.getEntries();
while(entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
System.out.println("Entry: "+entry.getName());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(unzipfolder+"\\"+entry.getName()));
BufferedInputStream bis = new BufferedInputStream(z.getInputStream(entry));
byte[] buffer=new byte[1000000];
int len=0;
while((len=bis.read(buffer,0,1000000))>0) {
bos.write(buffer, 0, len) ;
}
bis.close();
bos.close();
}
发布于 2020-12-17 13:57:25
我还用在示例站点上提供的"LZMA“代码(这还包括添加"xz”图书馆)甚至一个CompressorInputStream
来测试它,但是不管我做了什么,我总是得到一些类型的异常,例如:
org.tukaani.xz.UnsupportedOptionsException:未压缩的尺寸太大了
幸运的是,这方面有一个非正式的修复程序,发布为回答 for 这个问题。解释如下:
您的代码不能工作的原因是Zip压缩数据段与普通压缩LZMA文件有不同的头。
使用getInputstreamForEntry
(在答案中发布),我的代码现在能够同时处理zip存档中的LZMA和non文件:
ZipFile z = new ZipFile(zipfile);
Enumeration<ZipArchiveEntry> entries = z.getEntries();
while(entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
System.out.println("Entry: "+entry.getName());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(unzipfolder+"\\"+entry.getName()));
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(z.getInputStream(entry));
} catch(UnsupportedZipFeatureException e) {
bis = new BufferedInputStream(getInputstreamForEntry(z, entry));
}
byte[] buffer=new byte[1000000];
int len=0;
while((len=bis.read(buffer,0,1000000))>0) {
bos.write(buffer, 0, len) ;
}
bis.close();
bos.close();
}
https://stackoverflow.com/questions/65323801
复制相似问题