English 中文(简体)
Redirect unzip output to particular directory using Perl
原标题:
  • 时间:2009-12-16 19:22:16
  •  标签:
  • perl
  • unzip

I want to uncompress zipped file say, files.zip, to a directory that is different from my working directory. Say, my working directory is /home/user/address and I want to unzip files in /home/user/name.

I am trying to do it as follows

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

But when run the above from my working directory, all the files get unzipped in working directory. How do I redirect the uncompressed files to $newdir?

最佳回答
unzip $files -d $newdir
问题回答

Use Perl command

chdir $newdir;

and not the backticks

`cd $newdir`

which will just start a new shell, change the directory in that shell, and then exit.

Though for this example, the -d option to unzip is probably the simplest way to do what you want (as mentioned by ennuikiller), for other types of directory-changing, I like the File::chdir module, which allows you to localize directory changes, when combined with the perl "local" operator:

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

You can also use the Archive::Zip module. Look specifically at the extractToFileNamed:

"extractToFileNamed( $fileName )

Extract me to a file with the given name. The file will be created with default modes. Directories will be created as needed. The $fileName argument should be a valid file name on your file system. Returns AZ_OK on success. "





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签