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

Last change on this file since 244 was 244, checked in by nanardon, 18 years ago
  • lha list files differently if he archive come from DOS/Windows or unix, then adding a regexp to proper filter only files in the list for all case, ->ls() should no longer return files named "----------"
  • Property cvs2svn:cvs-rev set to 1.4
  • Property svn:keywords set to Author Date Id Revision
File size: 1.6 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        /^(\[\w*\]|(-|d|l|c|b)((-|r)(-|w)(-|x)){3} [^-])/ or next;
34        /^.{51}(.*)$/;
35        push(@list, $1);
36    }
37    close($hlha);
38    return @list;
39}
40
41sub extract {
42    my ($self, $file, $dest) = @_;
43
44    my $dir = tempdir();
45
46    if (system("lha xfw=$dir '$self->{archive}' '$file'")) {
47         $self->seterror("Can't uncompress archive: $!");
48         return undef;
49    }
50   
51    if ($dest) {
52        open(my $fh, '>', $dest) or do {
53            $self->seterror("Can't uncompress archive: $!");
54            return undef;
55        };
56        open(my $sourcefh, '<', "$dir/$file") or do {
57            $self->seterror("Can't open temp file for reading: $!");
58            return undef;
59        };
60
61        if(!copy($sourcefh, $fh)) {
62            $self->seterror("Can't copy file to destination: $!");
63            unlink("$dir/$file");
64            return undef;
65        }
66        close($fh);
67        close($sourcefh);
68    }
69   
70    return $dest || "$dir/$file";
71}
72
73ObsData::Archive::register(sub { $_[0] =~ /\.(lzh|lha)$/i }, 'Lha', 0);
Note: See TracBrowser for help on using the repository browser.