1 | # $Id$ |
---|
2 | |
---|
3 | package ObsData::Archive::Compressed; |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use File::Copy; |
---|
8 | use File::Temp qw/ tempfile /; |
---|
9 | use ObsData::Archive; |
---|
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 | |
---|
23 | bless($options, $class); |
---|
24 | } |
---|
25 | |
---|
26 | sub DESTROY { |
---|
27 | my ($self) = @_; |
---|
28 | } |
---|
29 | |
---|
30 | sub ls { |
---|
31 | my ($self) = @_; |
---|
32 | $self->{archive} =~ m,^(?:.*/)?(?:(.*)(?:\.(?:gz|bz2|Z))|(.*))$,; |
---|
33 | return $1 || $2; |
---|
34 | } |
---|
35 | |
---|
36 | sub extract { |
---|
37 | my ($self, $file, $dest) = @_; |
---|
38 | # the devel should specify the file he want |
---|
39 | # as the basic contains only 1 file... this does not matter |
---|
40 | |
---|
41 | my ($fh, $fname); |
---|
42 | |
---|
43 | if ($dest) { |
---|
44 | $fname = $dest; |
---|
45 | open($fh, '>', $dest) or do { |
---|
46 | $self->seterror("Can't uncompress archive: $!"); |
---|
47 | return undef; |
---|
48 | }; |
---|
49 | } else { |
---|
50 | ($fh, $fname) = tempfile( |
---|
51 | DIR => $self->_tempdir, |
---|
52 | UNLINK => 1, |
---|
53 | ) or do { |
---|
54 | $self->seterror("Can't create temp file: $!"); |
---|
55 | return undef; |
---|
56 | }; |
---|
57 | } |
---|
58 | |
---|
59 | open(my $sourcefh, "$self->{uncomp} '$self->{archive}' 2>/dev/null |") or return undef; |
---|
60 | |
---|
61 | if(!copy($sourcefh, $fh)) { |
---|
62 | $self->seterror("Can't copy file to destination: $!"); |
---|
63 | unlink($fname); |
---|
64 | return undef; |
---|
65 | } |
---|
66 | |
---|
67 | close($fh); |
---|
68 | if(!close($sourcefh)) { |
---|
69 | $self->seterror("$self->{uncomp} exit with error" . ($! ? (" " . $!) : "")); |
---|
70 | unlink($fname); |
---|
71 | return undef; |
---|
72 | } |
---|
73 | $fname |
---|
74 | } |
---|
75 | |
---|
76 | ObsData::Archive::register(sub { $_[0] =~ /\.(gz|Z|bz2)$/i }, 'Compressed', 5); |
---|