Last change
on this file since 152 was
152,
checked in by thauvin, 18 years ago
|
|
-
Property cvs2svn:cvs-rev set to
1.4
-
Property svn:keywords set to
Author Date Id Revision
|
File size:
1.7 KB
|
Line | |
---|
1 | # $Id$ |
---|
2 | |
---|
3 | package ObsData::Archive::Tar; |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use File::Copy; |
---|
8 | use File::Temp qw/ tempfile tempdir /; |
---|
9 | use Cwd; |
---|
10 | |
---|
11 | our @ISA = qw(ObsData::Archive); |
---|
12 | |
---|
13 | sub new { |
---|
14 | my ($class, $options) = @_; |
---|
15 | |
---|
16 | if (!$options->{uncomp}) { |
---|
17 | for ($options->{archive}) { |
---|
18 | /\.(Z|gz)$/ and $options->{uncomp} = 'gzip -dc'; |
---|
19 | /\.bz2$/ and $options->{uncomp} = 'bzip2 -dc'; |
---|
20 | } |
---|
21 | } |
---|
22 | $options->{uncomp} ||= 'cat'; |
---|
23 | bless($options, $class); |
---|
24 | } |
---|
25 | |
---|
26 | sub DESTROY { |
---|
27 | my ($self) = @_; |
---|
28 | } |
---|
29 | |
---|
30 | sub ls { |
---|
31 | my ($self) = @_; |
---|
32 | open(my $htar, '-|', "$self->{uncomp} '$self->{archive}' | tar tf -") or do { |
---|
33 | seterror("Can't read tar file: $!"); |
---|
34 | return undef; |
---|
35 | }; |
---|
36 | my @list; |
---|
37 | while(<$htar>) { |
---|
38 | chomp; |
---|
39 | push(@list, $_); |
---|
40 | } |
---|
41 | close($htar); |
---|
42 | return @list; |
---|
43 | } |
---|
44 | |
---|
45 | sub extract { |
---|
46 | my ($self, $file, $dest) = @_; |
---|
47 | |
---|
48 | my $tempdir = tempdir(); |
---|
49 | my $here = getcwd(); |
---|
50 | my $abs_path = Cwd::abs_path($self->{archive}); |
---|
51 | my $abs_dest = Cwd::abs_path($dest); |
---|
52 | |
---|
53 | if(!chdir($tempdir)) { |
---|
54 | $self->seterror("Can't chdir: $!"); |
---|
55 | return undef; |
---|
56 | } |
---|
57 | |
---|
58 | system("$self->{uncomp} '$abs_path' | tar xf - '$file'") and do { |
---|
59 | $self->seterror("Can't extract file from tar: $!"); |
---|
60 | chdir($here); |
---|
61 | return undef; |
---|
62 | }; |
---|
63 | |
---|
64 | if ($dest) { |
---|
65 | open(my $fh, '>', $abs_dest) or return undef; |
---|
66 | if(!copy("$tempdir/$file", $fh)) { |
---|
67 | $self->seterror("Can't copy file to destination: $!"); |
---|
68 | unlink($abs_dest); |
---|
69 | chdir($here); |
---|
70 | return undef; |
---|
71 | } |
---|
72 | close($fh); |
---|
73 | unlink("$tempdir/$file"); |
---|
74 | chdir($here); |
---|
75 | return $dest; |
---|
76 | } else { |
---|
77 | chdir($here); |
---|
78 | return "$tempdir/$file"; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | 1; |
---|
Note: See
TracBrowser
for help on using the repository browser.