2016-06-26 16 views
0

Also arbeite ich an das Extrahieren einer Datei.cbz, um seinen Inhalt anzuzeigen.Warum verhält sich dieser Befehl anders, je nachdem, ob er von terminal.app oder einem Scala-Programm aufgerufen wird?

ich es mit dem folgenden Code zu gehen hatte gehofft:

println("7z x -y \"" + filePath + "\"" + s" -o$tempLocation") 
     if (("7z x -y \"" + filePath + "\"" + s" -o$tempLocation").! == 0){ //I would have liked to do the whole thing with string interpolation, but I didn't work. Seems to be this bug https://issues.scala-lang.org/browse/SI-6476 
      Some(new File(tempLocation)) 
     }else{ 
     println("Something went wrong extracting the file, probably an incorrect path. Path: " + filePath) 
     null 
    } 

Als ich das kompilieren und ausführen, bekomme ich die folgende Ausgabe:

7z x -y "/path/to/file/test.cbz" -o/tmp/CViewer-temporary-storage 

ERROR: No more files 
" 



System ERROR: 
Unknown error: -2147024872 

7-Zip [64] 15.14 : Copyright (c) 1999-2015 Igor Pavlov : 2015-12-31 
p7zip Version 15.14.1 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64) 

Scanning the drive for archives: 
Something went wrong extracting the file, probably an incorrect path. Path: /Users/Matt/learning-scala/learning-GUI/test.cbz 

Process finished with exit code 0 

Wenn ich den Befehl im Terminal laufen .app, es funktioniert gut, und ich folgendes:

Matt$ 7z x -y "/Users/Matt/learning-scala/learning-GUI/test.cbz" -o/tmp/CViewer-temporary-storage 

7-Zip [64] 15.14 : Copyright (c) 1999-2015 Igor Pavlov : 2015-12-31 
p7zip Version 15.14.1 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64) 

Scanning the drive for archives: 
1 file, 19587584 bytes (19 MiB)     

Extracting archive: /path/to/file/test.cbz 
-- 
Path = /path/to/file/test.cbz 
Type = Rar 
Physical Size = 19587584 
Solid = - 
Blocks = 33 
Multivolume = - 
Volumes = 1 

Everything is Ok 

Files: 33 
Size:  20400561 
Compressed: 19587584 

, dass das Ergebnis zu sein scheint, wenn 7z zu extr erzählt handle ein leeres Verzeichnis.

Antwort

2

Sie scheinen zu denken, Scala unterstützt Bash-ähnliche Syntax und das Aufheben von Regeln. Von dem, was ich sagen kann Scalas ProcessBuilder hat keine Syntax für das Entsperren von Strings, wenn Sie ! auf einem String aufrufen. Was es stattdessen zu tun scheint, wird naiv in Whitespaces aufgeteilt und führt diese Teile dann als Befehl aus. Dies funktioniert gut, wenn Sie in Ihrem Befehl keine Leerzeichen einfügen müssen oder wenn Ihre interpolierten Variablen niemals Leerzeichen enthalten.

Die Lösung besteht darin, Seq zu verwenden, so dass Sie jedes Argument einzeln definieren können, ohne sich um das Entweichen oder Unscaping kümmern zu müssen. So Sie exec line:

"7z x -y \"" + filePath + "\"" + s" -o$tempLocation").! 

wird

Seq("7z", "x" "-y", filePath, s"-o$tempLocation").! 

Hinweis: pro scripting Best Practices Sie immer den vollständigen Pfad zum Befehl verwenden, sollten Sie ausführen. Für mich wäre das:

Seq("/usr/local/bin/7z", "x" "-y", filePath, s"-o$tempLocation").!