source: trunk/soft/ObsData/ObsData/Archive/Compressed.pm @ 152

Last change on this file since 152 was 152, checked in by thauvin, 18 years ago
  • fix call to seterror
  • 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
3package ObsData::Archive::Compressed;
4
5use strict;
6use warnings;
7use File::Copy;
8use File::Temp qw/ tempfile /;
9
10our @ISA = qw(ObsData::Archive);
11
12sub 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
25sub DESTROY {
26    my ($self) = @_;
27}
28
29sub ls {
30    my ($self) = @_;
31    $self->{archive} =~ m,^(?:.*/)?(?:(.*)(?:\.(?:gz|bz2|Z))|(.*))$,;
32    return $1 || $2;
33}
34
35sub 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
751;
Note: See TracBrowser for help on using the repository browser.