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