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

Last change on this file since 9 was 9, checked in by thauvin, 19 years ago
  • add tar support
  • Property cvs2svn:cvs-rev set to 1.5
  • Property svn:keywords set to Author Date Id Revision
File size: 1.7 KB
Line 
1# $Id$
2
3package ObsData::Archive;
4
5use strict;
6use warnings;
7use File::Copy;
8use File::Temp qw/ tempfile /;
9use ObsData::Archive::Compressed;
10use ObsData::Archive::Tar;
11
12sub new {
13    my ($class, $archive, %options) = @_;
14    my $beclass;
15
16    my $o;
17   
18    foreach (keys %options) {
19        $o->{$_} = $options{$_};
20    }
21    $o->{archive} = $archive;
22   
23    for ($archive) {
24        /\.tar(\.[^\.]*)?$/ and do {
25            $beclass = 'Tar';
26            last;
27        };
28        /\.(gz|bz2|Z)$/ and do {
29            $beclass = 'Compressed';
30            last;
31        };
32    }
33   
34    if ($beclass) {
35        my $obj;
36        # eval("require $class\:\:$beclass;");
37        eval("\$obj = $class\:\:$beclass->new(\$o);");
38        return $obj;
39    } else {
40        bless($o, $class);
41    }
42}
43
44sub DESTROY {
45    my ($self) = @_;
46}
47
48sub ls {
49    my ($self) = @_;
50    my ($file) = $self->{archive} =~ m!^(?:.*/)?(.*)!;
51    return $file;
52}
53
54sub extract {
55    my ($self, $file, $dest) = @_;
56    # the devel should specify the file he want
57    # as the basic contains only 1 file... this does not matter
58
59    my ($fh, $fname);
60   
61    if ($dest) {
62        $fname = $dest;
63        open($fh, '>', $dest) or return undef;
64    } else {
65        ($fh, $fname) = tempfile(
66            DIR => $self->_tempdir,
67            UNLINK => 1,
68        ) or return undef;
69    }
70   
71    if(!copy($self->{archive}, $fh)) {
72        unlink($fname);
73        return undef;
74    }
75   
76    close($fh);
77    $fname
78}
79
80### Private method
81
82sub _tempdir {
83    my ($self) = @_;
84    $self->{tmpdir} || $ENV{TMPDIR}
85}
86
871;
88
89__END__
90
91=HEAD1 Method
92
93=item new
94
95Create a new archive object
96
97=item ls
98
99list the content of the archive
100
101=item extract
102
103Extract a file from the archive and return the filename of extract file
Note: See TracBrowser for help on using the repository browser.