简体   繁体   English

以编程方式执行解压缩命令

[英]Executing unzip command programmatically

I have created a shell script and inside of it is a simple statement unzip -o $1 and on running through terminal and passing a .zip file as parameter it works fine and takes 5 second to create unzipped folder.Now I am trying to do the same thing in scala and my code is as below : 我已经创建了一个shell脚本,它的内部是一个简单的语句unzip -o $1 ,在运行终端并传递.zip文件作为参数时,它可以正常工作,并且需要5秒钟来创建未压缩的文件夹。在scala中同样的事情,我的代码如下:

object ZipExt extends App {

  val process = Runtime.getRuntime.exec(Array[String]("/home/administrator/test.sh", "/home/administrator/MyZipFile_0.8.6.3.zip"))
  process.waitFor

  println("done")
}

Now whenever I am trying to execute ZipExt it gets stuck in process.waitFor forever and print statement is not reached.I have tried using this code both locally and on server also. 现在,每当我尝试执行ZipExt时,它就会卡在process.waitFor并且永远不会到达print语句。我尝试在本地和服务器上都使用此代码。 I tried other possibilites also like creating local variable inside shellscript, including exit statements inside shell script, trying to unzip other .zip other than mines, even sometimes print statement is executing but no unzipped file is created there. 我尝试了其他可能性,例如在shellscript中创建局部变量,包括shell脚本中的exit语句,试图解压缩除我的之外的其他.zip ,甚至有时执行print语句,但未在其中创建解压缩的文件。 So I am pretty sure there is something wrong about executing unzip command programmatically to unzip a file or there is some other way around to unzip a zipped file programtically.I have been stuck with this problem for like 2 days, so somebody plz help.. 所以我很确定以编程方式执行unzip命令来解压缩文件有问题,或者有其他方法可以以编程方式解压缩压缩后的文件。我被这个问题困扰了大约2天,所以请plz帮助。

The information you have given us appears to be insufficient to reproduce the problem: 您提供给我们的信息似乎不足以重现该问题:

% mkdir 34088099
% cd 34088099 
% mkdir junk
% touch junk/a junk/b junk/c
% zip -r junk.zip junk
updating: junk/ (stored 0%)
  adding: junk/a (stored 0%)
  adding: junk/b (stored 0%)
  adding: junk/c (stored 0%)
% rm -r junk
% echo 'unzip -o $1' > test.sh
% chmod +x test.sh
% scala
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val process = Runtime.getRuntime.exec(Array[String]("./test.sh", "junk.zip"))
process: Process = java.lang.UNIXProcess@35432107

scala> process.waitFor
res0: Int = 0

scala> :quit
% ls junk 
a  b  c

I would suggest trying this same reproduction on your own machine. 我建议您在自己的机器上尝试同样的复制。 If it succeeds for you too, then start systematically reducing the differences between the succeeding case and the failing case, a step at a time. 如果它也为您成功,则可以系统地减少成功案例和失败案例之间的差异,一次一步。 This will help narrow down what the possible causes are. 这将有助于缩小可能原因的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM