2010-12-30 3 views
2

Windows 플랫폼에서 Java 프로그램을 작성하고 있습니다. 특정 파일을 압축 파일로 압축해야합니다. 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/

관련 문제