【问题标题】:7Zip does not exit after processing large zip file compression处理大型 zip 文件压缩后 7Zip 不退出
【发布时间】:2010-12-30 16:33:35
【问题描述】:

我正在windows平台上写一个java程序。我需要将某些文件压缩到一个 zip 存档中。我正在使用 ProcessBuilder 启动一个新的 7zip 进程:

ProcessBuilder processBuilder = new ProcessBuilder("7Z","a",zipPath,filePath);
Process p = processBuilder.start();
p.waitFor();

问题是 7zip 进程在完成后永远不会退出。它确实创建了所需的 zip 文件,但之后就挂在那里了。这意味着waitFor() 调用永远不会返回并且我的程序卡住了。请提出修复或解决方法。

【问题讨论】:

  • 有时调用进程的问题是您需要处理/清除产品的输出。一旦他们的输出缓冲区满了,他们就会等待缓冲区再次空闲..
  • 你知道 Java 有一个 zip 包来读/写 zip 文件吗? java.sun.com/developer/technicalArticles/Programming/…
  • 谢谢你修复它。我只是将输出重定向到一个文件
  • @user434541:您可以回答自己的问题。只需将您的解决方案放在这里并将其标记为已接受的解决方案。这样,您将帮助其他人在未来找到解决方案并清理 SO 未决问题队列:)。

标签: java 7zip processbuilder


【解决方案1】:

这就是我最终要做的。

我无法设置环境变量,所以我必须为 7zip 设置 c: 路径。

    public void zipMultipleFiles (List<file> Files, String destinationFile){
        String zipApplication = "\"C:\\Program Files\7Zip\7zip.exe\" a -t7z"; 
        String CommandToZip = zipApplication + " ";    
        for (File file : files){
           CommandToZip = CommandToZip + "\"" + file.getAbsolutePath() + "\" ";
        }
        CommandToZip = CommandToZip + " -mmt -mx5 -aoa";
        runCommand(CommandToZip);
    }

    public void runCommand(String commandToRun) throws RuntimeException{
        Process p = null;
        try{
            p = Runtime.getRuntime().exec(commandToRun);
            String response = convertStreamToStr(p.getInputStream());
            p.waitFor();
        } catch(Exception e){
            throw new RuntimeException("Illegal Command ZippingFile");
        } finally {
            if(p = null){
                throw new RuntimeException("Illegal Command Zipping File");
            }
            if (p.exitValue() != 0){
                throw new Runtime("Failed to Zip File - unknown error");
            }
        }
    }

可以在这里找到转换为字符串的函数,这是我用作参考的。 http://singztechmusings.wordpress.com/2011/06/21/getting-started-with-javas-processbuilder-a-sample-utility-class-to-interact-with-linux-from-java-program/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多