source: trunk/soft/ObsData/ObsData/Archive.pm @ 161

Last change on this file since 161 was 161, checked in by thauvin, 19 years ago
  • cleanup
  • Property cvs2svn:cvs-rev set to 1.12
  • Property svn:keywords set to Author Date Id Revision
File size: 2.7 KB
Line 
1# $Id$
2
3package ObsData::Archive;
4
5use strict;
6use warnings;
7use ObsData::Archive::FlatFile;
8use ObsData::Archive::Compressed;
9use ObsData::Archive::Tar;
10use ObsData::Archive::Zip;
11use ObsData::Archive::Rar;
12
13our $CVSID = q$Id$;
14our $CVSREV = (q$Revision$ =~ /^Revision: (.*) $/)[0];
15
16my $error = {};
17
18sub new {
19    my ($class, $archive, %options) = @_;
20    my $beclass;
21
22    if (!$archive) {
23        seterror("No archive to read");
24        return undef;
25    }
26    if (!-r $archive) {
27        seterror("No such file or directory");
28        return undef;
29    }
30    my $o;
31   
32    foreach (keys %options) {
33        $o->{$_} = $options{$_};
34    }
35    $o->{archive} = $archive;
36   
37    for ($archive) {
38        /\.tar(\.[^\.]*)?$/ and do {
39            $beclass = 'Tar';
40            last;
41        };
42        /\.(gz|bz2|Z)$/ and do {
43            $beclass = 'Compressed';
44            last;
45        };
46        /\.zip$/ and do {
47            $beclass = 'Zip';
48            last;
49        };
50        /\.rar$/ and do {
51            $beclass = 'Rar';
52            last;
53        };
54        $beclass = 'FlatFile';
55    }
56   
57    if ($beclass) {
58        my $obj;
59        # eval("require $class\:\:$beclass;");
60        eval("\$obj = $class\:\:$beclass->new(\$o);");
61        return $obj;
62    } else {
63        bless($o, $class);
64    }
65}
66
67sub DESTROY {
68    my ($self) = @_;
69}
70
71sub ls {
72    my ($self) = @_;
73    seterror("ls not implement in class " . ref($self));
74    return;
75}
76
77sub extract {
78    my ($self, $file, $dest) = @_;
79    seterror("extract not implement in class " . ref($self));
80    return;
81}
82
83### Global functions
84
85sub error {
86    return $error->{error};
87}
88
89sub seterror {
90    my ($package, $filename, $line) = caller;
91    $error = {
92        'package' => $package,
93        'filename' => $filename,
94        'line' => $line,
95        'error' => $_[1] || $_[0],
96    };
97}
98
99### Private method
100
101sub _tempdir {
102    my ($self) = @_;
103    $self->{tmpdir} || $ENV{TMPDIR}
104}
105
1061;
107
108__END__
109
110=head1 NAME
111
112ObsData::Archive - Transparently extract files from arbitrary archives
113
114=head1 SYNOPSIS
115
116    use ObsData::Archive;
117    my $file = "foo.tar.gz";
118    my $oa = ObsData::Archive->new($file) or die ObsData::Archive->error;
119    my @content = $oa->ls;
120    foreach (@content) {
121        $oa->extract($_, "./$_") or die ObsData::Archive->error;
122    }
123
124=head1 METHODS
125
126=head2 new($file, %options)
127
128Create a new archive object by reading $file.
129
130Return undef on error and set error status.
131See L<error>.
132
133=head2 ls
134
135list the content of the archive
136
137=head2 extract
138
139Extract a file from the archive and return the filename of extract file
140
141=head1 CLASS METHODS
142
143=head2 error
144
145Return the last error encoutered
146
147=head1 AUTHORS
148
149Olivier Thauvin <olivier.thauvin@aerov.jussieu.fr>
150
151=cut
Note: See TracBrowser for help on using the repository browser.