source: trunk/soft/ObsData/ObsData/Archive/Zip.pm @ 160

Last change on this file since 160 was 160, checked in by thauvin, 18 years ago
  • clean up
  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:keywords set to Author Date Id Revision
File size: 1.5 KB
Line 
1# $Id$
2
3package ObsData::Archive::Zip;
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    bless($options, $class);
16}
17
18sub DESTROY_ {
19    my ($self) = @_;
20}
21
22sub ls {
23    my ($self) = @_;
24    open(my $hzip, '-|', "zipinfo -1 '$self->{archive}'") or do {
25        $self->seterror("Can't read zip file: $!");
26        return undef;
27    };
28    my @list;
29    while(<$hzip>) {
30        chomp;
31        push(@list, $_);
32    }
33    close($hzip);
34    return @list;
35}
36
37sub extract {
38    my ($self, $file, $dest) = @_;
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, "unzip -p '$self->{archive}' '$file' 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("unzip -p exit with error" . ($! ? (" " . $!) : ""));
69        unlink($fname);
70        return undef;
71    }
72    $fname
73
74}
75
761;
Note: See TracBrowser for help on using the repository browser.