-2

我想将压缩文件解压缩files.zip到与我的工作目录不同的目录。说,我的工作目录是/home/user/address,我想将文件解压缩到/home/user/name.

我正在尝试按如下方式进行

#!/usr/bin/perl
use strict;
use warnings;

my $files= "/home/user/name/files.zip"; #location of zip file
my $wd = "/home/user/address" #working directory
my $newdir= "/home/user/name"; #directory where files need to be extracted
my $dir = `cd $newdir`;
my @result = `unzip $files`; 

但是,当从我的工作目录运行上述文件时,所有文件都会在工作目录中解压缩。如何将未压缩的文件重定向到$newdir?

4

4 回答 4

8
unzip $files -d $newdir
于 2009-12-16T19:25:42.523 回答
3

使用 Perl 命令

chdir $newdir;

而不是反引号

`cd $newdir`

它只会启动一个新的 shell,更改该 shell 中的目录,然后退出。

于 2009-12-16T19:34:32.047 回答
1

虽然对于这个例子,解压缩的 -d 选项可能是做你想做的最简单的方法(正如 ennukiller 所提到的),对于其他类型的目录更改,我喜欢 File::chdir 模块,它允许你本地化与 perl “local” 运算符结合使用时,目录更改:

#!/usr/bin/perl
use strict;
use warnings;
use File::chdir;

my $files= "/home/user/name/files.zip"; #location of zip file
my $wd = "/home/user/address" #working directory
my $newdir= "/home/user/name"; #directory where files need to be extracted
# doesn't work, since cd is inside a subshell:   my $dir = `cd $newdir`;
{ 
   local $CWD = $newdir;
   # Within this block, the current working directory is $newdir
   my @result = `unzip $files`;
}
# here the current working directory is back to what it was before
于 2009-12-16T20:28:24.873 回答
0

您还可以使用 Archive::Zip 模块。具体看extractToFileNamed:

"extractToFileNamed($fileName)

将我提取到具有给定名称的文件中。该文件将使用默认模式创建。将根据需要创建目录。$fileName 参数应该是文件系统上的有效文件名。成功时返回 AZ_OK。"

于 2009-12-16T19:48:56.343 回答