Question

Je suis en train d'écrire un programme Java sur la plate-forme Windows. Je dois compresser certains fichiers dans une archive zip. J'utilise ProcessBuilder pour lancer un nouveau processus de 7zip:

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

Le problème est que le processus de 7zip ne sort après la fin. Il ne crée le fichier zip requis, mais après que seulement se bloque là-dedans. Cela signifie que l'appel ne retourne jamais waitFor() et mon programme est bloqué. S'il vous plaît suggérer une solution ou un travail autour.

Était-ce utile?

La solution

voici ce que je fini par faire.

Je ne peux pas définir des variables de eNViroNNeMeNt donc je devais régler le c:. Chemin pour 7zip

    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");
            }
        }
    }

La conversion à la fonction de chaîne peut être trouvée ici qui est ce que j'ai utilisé comme une référence. http://singztechmusings.wordpress.com/2011/06/21/getting-started-with-javas-processbuilder-a-sample-utility-class-to-interact-with- linux-de-java-programme /

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top