首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Apache : UnsupportedZipFeatureException (LZMA)

Apache : UnsupportedZipFeatureException (LZMA)
EN

Stack Overflow用户
提问于 2020-12-16 13:00:12
回答 1查看 456关注 0票数 0

我想解压缩使用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库完全解压缩这个归档?这可能吗?

我的代码:

代码语言:javascript
运行
复制
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();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-17 13:57:25

我还用在示例站点上提供的"LZMA“代码(这还包括添加"xz”图书馆)甚至一个CompressorInputStream来测试它,但是不管我做了什么,我总是得到一些类型的异常,例如:

org.tukaani.xz.UnsupportedOptionsException:未压缩的尺寸太大了

幸运的是,这方面有一个非正式的修复程序,发布为回答 for 这个问题。解释如下:

您的代码不能工作的原因是Zip压缩数据段与普通压缩LZMA文件有不同的头。

使用getInputstreamForEntry (在答案中发布),我的代码现在能够同时处理zip存档中的LZMA和non文件:

代码语言:javascript
运行
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65323801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档