source: trunk/soft/ObsData/ObsData/Archive/Rar.pm @ 157

Last change on this file since 157 was 157, checked in by thauvin, 18 years ago
  • add Rar support
  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:keywords set to Author Date Id Revision
File size: 1.5 KB
Line 
1# $Id$
2
3package ObsData::Archive::Rar;
4
5use strict;
6use warnings;
7use File::Copy;
8use File::Temp qw/ tempfile tempdir /;
9use Cwd;
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 $hrar, '-|', "unrar lb '$self->{archive}'") or do {
25        $self->seterror("Can't read zip file: $!");
26        return undef;
27    };
28    my @list;
29    while(<$hrar>) {
30        chomp;
31        push(@list, $_);
32    }
33    close($hrar);
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, "unrar p -ierr '$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    # FIXME unrar sucks, does return a proper exit status on error (allways 0)
68    if(!close($sourcefh)) {
69        $self->seterror("unrar -p exit with error" . ($! ? (" " . $!) : ""));
70        unlink($fname);
71        return undef;
72    }
73    $fname
74}
75
761;
Note: See TracBrowser for help on using the repository browser.