2010-12-30 29 views
2

WindowsプラットフォームでJavaプログラムを作成しています。特定のファイルをzipアーカイブに圧縮する必要があります。私はProcessBuilderを使用して新しい7zipプロセスを開始します:大きなzipファイルの圧縮処理後に7Zipが終了しない

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

問題は、7zipプロセスが完了後に終了しないことです。それは必要なzipファイルを作成しますが、その後はそこにハングアップします。つまり、waitFor()コールは返されず、プログラムが停止します。修正または回避策を提案してください。

+0

時々、プロセスを呼び出す際の問題は、プロダクションの出力を処理/クリアする必要があることです。出力バッファがいっぱいになると、バッファが再び解放されるのを待ちます。 – bert

+1

Javaにはzipファイルを読み書きするためのzipパッケージがありますか? http://java.sun.com/developer/technicalArticles/Programming/compression/ –

+1

固定していただきありがとうございます。出力をファイルにリダイレクトしただけです。 – user434541

答えて

2

ここは私がやったことです。

私は環境変数を設定できませんので、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/

関連する問題