2009-12-16 31 views
-2

我想解压压缩文件,比如files.zip,到一个与我的工作目录不同的目录。 说,我的工作目录是/home/user/address,我想解压缩文件/home/user/name使用Perl重定向解压输出到特定的目录

我想如下

#!/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

+2

如果你想改变你的工作目录,看看chdir函数http://perldoc.perl.org/functions/chdir.html – Nifle 2009-12-16 19:34:53

+1

这个问题根本不是Perl相关的。事实上,这根本不是一个编程问题。这是一个关于'unzip'的命令行选项的问题,你可以在命令行上输入unzip并按输入。 – 2009-12-16 20:38:48

+0

@Sinan:这是Perl相关的,因为我也想知道Perl中的哪个命令改变了这个目录。正如mobrule所说,它是chdir。如果不是他,我不会知道这件事。 – shubster 2009-12-18 12:42:11

回答

8
unzip $files -d $newdir 
+0

我在哪里可以阅读文档'd'? – shubster 2009-12-16 19:30:31

+4

man unzip ........... – 2009-12-16 19:31:29

+0

你不需要'man'。只需在命令行键入'unzip'并按下'Enter'。 – 2009-12-16 20:39:19

3

使用Perl命令

chdir $newdir; 

,而不是反引号

`cd $newdir` 

这将引起新的外壳,将目录更改在壳,然后退出。

0

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

“extractToFileNamed($文件名)

提取我与给定名称的文件,该文件将使用默认模式下创建的目录将创建为需要$ filename参数“

1

尽管对于这个例子,解压缩的-d选项可能是做你想做的事情的最简单的方法(如ennuikiller所提到的),对于其他类型的目录更改,我喜欢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