Last change
on this file since 9 was
9,
checked in by thauvin, 19 years ago
|
|
-
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 | |
---|
3 | package ObsData::Archive; |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use File::Copy; |
---|
8 | use File::Temp qw/ tempfile /; |
---|
9 | use ObsData::Archive::Compressed; |
---|
10 | use ObsData::Archive::Tar; |
---|
11 | |
---|
12 | sub 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 | |
---|
44 | sub DESTROY { |
---|
45 | my ($self) = @_; |
---|
46 | } |
---|
47 | |
---|
48 | sub ls { |
---|
49 | my ($self) = @_; |
---|
50 | my ($file) = $self->{archive} =~ m!^(?:.*/)?(.*)!; |
---|
51 | return $file; |
---|
52 | } |
---|
53 | |
---|
54 | sub 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 | |
---|
82 | sub _tempdir { |
---|
83 | my ($self) = @_; |
---|
84 | $self->{tmpdir} || $ENV{TMPDIR} |
---|
85 | } |
---|
86 | |
---|
87 | 1; |
---|
88 | |
---|
89 | __END__ |
---|
90 | |
---|
91 | =HEAD1 Method |
---|
92 | |
---|
93 | =item new |
---|
94 | |
---|
95 | Create a new archive object |
---|
96 | |
---|
97 | =item ls |
---|
98 | |
---|
99 | list the content of the archive |
---|
100 | |
---|
101 | =item extract |
---|
102 | |
---|
103 | Extract a file from the archive and return the filename of extract file |
---|
Note: See
TracBrowser
for help on using the repository browser.