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

Last change on this file since 241 was 241, checked in by nanardon, 18 years ago
  • end lha support, all test should pass now
  • Property cvs2svn:cvs-rev set to 1.4
  • Property svn:keywords set to Author Date Id Revision
File size: 1.5 KB
Line 
1# $Id$
2
3package ObsData::Archive::Lha;
4
5use strict;
6use warnings;
7use File::Copy;
8use File::Temp qw/ tempdir /;
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 $dir = tempdir();
44
45    if (system("lha xfw=$dir '$self->{archive}' '$file'")) {
46         $self->seterror("Can't uncompress archive: $!");
47         return undef;
48    }
49   
50    if ($dest) {
51        open(my $fh, '>', $dest) or do {
52            $self->seterror("Can't uncompress archive: $!");
53            return undef;
54        };
55        open(my $sourcefh, '<', "$dir/$file") or do {
56            $self->seterror("Can't open temp file for reading: $!");
57            return undef;
58        };
59
60        if(!copy($sourcefh, $fh)) {
61            $self->seterror("Can't copy file to destination: $!");
62            unlink("$dir/$file");
63            return undef;
64        }
65        close($fh);
66        close($sourcefh);
67    }
68   
69    return $dest || "$dir/$file";
70}
71
72ObsData::Archive::register(sub { $_[0] =~ /\.(lzh|lha)$/i }, 'Lha', 0);
Note: See TracBrowser for help on using the repository browser.