【问题标题】:SevenZFile - Apache Commons Compress 1.15, UncompressSevenZFile - Apache Commons Compress 1.15,解压缩
【发布时间】:2018-01-25 12:21:49
【问题描述】:

在解压 .7z 文件时,空文件夹会被忽略,我想在解压任何 .7z 文件后也考虑空文件夹。

我的代码如下

SevenZFile sevenZFile = new SevenZFile(new File(filename));
SevenZArchiveEntry entry;

while ((entry = sevenZFile.getNextEntry()) != null){
    if (entry.isDirectory()){
        continue;
    }

    File curfile = new File(DestinationPath,entry.getName());
    File parent = curfile.getParentFile();

    if (!parent.exists()) {
        parent.mkdirs();
    }

    FileOutputStream out = new FileOutputStream(curfile);
    byte[] content = new byte[(int) entry.getSize()];
    sevenZFile.read(content, 0, content.length);
    out.write(content);
    out.close();

【问题讨论】:

    标签: java extract 7zip compression


    【解决方案1】:

    您的代码似乎可以正常工作。

    可能该文件夹从一开始就不在“yourfile.7zip”中。

    这是 7zip 的常见问题,您必须更新您的 7zip 版本。

    如果 7Zip 包含正确的参数,请使用:

     if (entry.isDirectory()){
                     new File(DestinationPath,entry.getName()).mkdir(); 
                    continue;
                }
    

    自:

    文件输出流是用于将数据写入文件或 到一个 FileDescriptor。

    这是完成任务的正确方法,因为本机库供应商没有实现文件夹。

    【讨论】:

    • 7z 文件中有一些空文件夹,不幸的是,程序会忽略空文件夹。
    • 嗯...好吧,给我一点时间重新排列答案。
    • 更新了我的答案。
    • 谢谢,BlackJack,代码可以工作,但它正在创建空文件夹到第 2 级,它不会更进一步。
    猜你喜欢
    • 2013-09-04
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2015-03-25
    相关资源
    最近更新 更多