Last change
on this file since 152 was
152,
checked in by thauvin, 18 years ago
|
|
-
Property cvs2svn:cvs-rev set to
1.6
-
Property svn:keywords set to
Author Date Id Revision
|
File size:
1.6 KB
|
Line | |
---|
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 | |
---|
10 | our @ISA = qw(ObsData::Archive); |
---|
11 | |
---|
12 | sub new { |
---|
13 | my ($class, $options) = @_; |
---|
14 | |
---|
15 | if (!$options->{uncomp}) { |
---|
16 | for ($options->{archive}) { |
---|
17 | /\.(Z|gz)$/ and $options->{uncomp} = 'gzip -dc'; |
---|
18 | /\.bz2$/ and $options->{uncomp} = 'bzip2 -dc'; |
---|
19 | } |
---|
20 | } |
---|
21 | |
---|
22 | bless($options, $class); |
---|
23 | } |
---|
24 | |
---|
25 | sub DESTROY { |
---|
26 | my ($self) = @_; |
---|
27 | } |
---|
28 | |
---|
29 | sub ls { |
---|
30 | my ($self) = @_; |
---|
31 | $self->{archive} =~ m,^(?:.*/)?(?:(.*)(?:\.(?:gz|bz2|Z))|(.*))$,; |
---|
32 | return $1 || $2; |
---|
33 | } |
---|
34 | |
---|
35 | sub extract { |
---|
36 | my ($self, $file, $dest) = @_; |
---|
37 | # the devel should specify the file he want |
---|
38 | # as the basic contains only 1 file... this does not matter |
---|
39 | |
---|
40 | my ($fh, $fname); |
---|
41 | |
---|
42 | if ($dest) { |
---|
43 | $fname = $dest; |
---|
44 | open($fh, '>', $dest) or do { |
---|
45 | $self->seterror("Can't uncompress archive: $!"); |
---|
46 | return undef; |
---|
47 | }; |
---|
48 | } else { |
---|
49 | ($fh, $fname) = tempfile( |
---|
50 | DIR => $self->_tempdir, |
---|
51 | UNLINK => 1, |
---|
52 | ) or do { |
---|
53 | $self->seterror("Can't create temp file: $!"); |
---|
54 | return undef; |
---|
55 | }; |
---|
56 | } |
---|
57 | |
---|
58 | open(my $sourcefh, "$self->{uncomp} '$self->{archive}' 2>/dev/null |") or return undef; |
---|
59 | |
---|
60 | if(!copy($sourcefh, $fh)) { |
---|
61 | $self->seterror("Can't copy file to destination: $!"); |
---|
62 | unlink($fname); |
---|
63 | return undef; |
---|
64 | } |
---|
65 | |
---|
66 | close($fh); |
---|
67 | if(!close($sourcefh)) { |
---|
68 | $self->seterror("$self->{uncomp} exit with error" . ($! ? (" " . $!) : "")); |
---|
69 | unlink($fname); |
---|
70 | return undef; |
---|
71 | } |
---|
72 | $fname |
---|
73 | } |
---|
74 | |
---|
75 | 1; |
---|
Note: See
TracBrowser
for help on using the repository browser.