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

Last change on this file since 162 was 162, checked in by thauvin, 19 years ago
  • make Archive dynamique
  • Property cvs2svn:cvs-rev set to 1.8
  • Property svn:keywords set to Author Date Id Revision
File size: 1.7 KB
Line 
1# $Id$
2
3package ObsData::Archive::Compressed;
4
5use strict;
6use warnings;
7use File::Copy;
8use File::Temp qw/ tempfile /;
9use ObsData::Archive;
10
11our @ISA = qw(ObsData::Archive);
12
13sub 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
26sub DESTROY {
27    my ($self) = @_;
28}
29
30sub ls {
31    my ($self) = @_;
32    $self->{archive} =~ m,^(?:.*/)?(?:(.*)(?:\.(?:gz|bz2|Z))|(.*))$,;
33    return $1 || $2;
34}
35
36sub 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
76ObsData::Archive::register(sub { $_[0] =~ /\.(gz|Z|bz2)$/i }, 'Compressed', 5);
Note: See TracBrowser for help on using the repository browser.