source: trunk/soft/ObsData/ObsData/Archive/Lha.pm @ 240

Last change on this file since 240 was 240, checked in by nanardon, 18 years ago
  • add partial lha/lzh support
  • Property cvs2svn:cvs-rev set to 1.4
  • Property svn:keywords set to Author Date Id Revision
File size: 1.7 KB
Line 
1# $Id$
2
3package ObsData::Archive::Lha;
4
5use strict;
6use warnings;
7use File::Copy;
8use File::Temp qw/ tempfile /;
9use ObsData::Archive;
10
11our @ISA = qw(ObsData::Archive::template);
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 $hlha, '-|', "lha '$self->{archive}'") or do {
25        $self->seterror("Can't read lha/lzh file: $!");
26        return undef;
27    };
28    my @list;
29    <$hlha>;
30    <$hlha> =~ /^-{9}/ or return;
31    while(<$hlha>) {
32        chomp;
33        /^.{51}(.*)$/;
34        push(@list, $1);
35    }
36    close($hlha);
37    return @list;
38}
39
40sub extract {
41    my ($self, $file, $dest) = @_;
42
43    my ($fh, $fname);
44   
45    if ($dest) {
46        $fname = $dest;
47        open($fh, '>', $dest) or do {
48            $self->seterror("Can't uncompress archive: $!");
49            return undef;
50        };
51    } else {
52        ($fh, $fname) = tempfile(
53            DIR => $self->_tempdir,
54            UNLINK => 1,
55        ) or do {
56            $self->seterror("Can't create temp file: $!");
57            return undef;
58        };
59    }
60
61    open(my $sourcefh, "unrar p -ierr '$self->{archive}' '$file' 2>/dev/null |") or return undef;
62   
63    if(!copy($sourcefh, $fh)) {
64        $self->seterror("Can't copy file to destination: $!");
65        unlink($fname);
66        return undef;
67    }
68   
69    close($fh);
70    # FIXME unrar sucks, does return a proper exit status on error (allways 0)
71    if(!close($sourcefh)) {
72        $self->seterror("unrar -p exit with error" . ($! ? (" " . $!) : ""));
73        unlink($fname);
74        return undef;
75    }
76    $fname
77}
78
79ObsData::Archive::register(sub { $_[0] =~ /\.(lzh|lha)$/i }, 'Lha', 0);
Note: See TracBrowser for help on using the repository browser.