1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # ZIPPlug.pm --
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 | # plugin which handles compressed and/or archived input formats
|
---|
27 | #
|
---|
28 | # currently handled formats and file extensions are:
|
---|
29 | #
|
---|
30 | # gzip (.gz, .z, .tgz, .taz)
|
---|
31 | # bzip (.bz)
|
---|
32 | # zip (.zip)
|
---|
33 | # tar (.tar)
|
---|
34 | #
|
---|
35 | # this plugin relies on the following utilities being present
|
---|
36 | # (if trying to process the corresponding formats)
|
---|
37 | #
|
---|
38 | # gunzip (for gzip)
|
---|
39 | # bunzip (for bzip)
|
---|
40 | # unzip (for zip)
|
---|
41 | # tar (for tar)
|
---|
42 |
|
---|
43 | package ZIPPlug;
|
---|
44 |
|
---|
45 | use BasPlug;
|
---|
46 | use plugin;
|
---|
47 | use util;
|
---|
48 | use Cwd;
|
---|
49 |
|
---|
50 |
|
---|
51 | BEGIN {
|
---|
52 | @ISA = ('BasPlug');
|
---|
53 | }
|
---|
54 |
|
---|
55 | sub new {
|
---|
56 | my ($class) = @_;
|
---|
57 | my $self = new BasPlug ("ZIPPlug", @_);
|
---|
58 |
|
---|
59 | return bless $self, $class;
|
---|
60 | }
|
---|
61 |
|
---|
62 | # this is a recursive plugin
|
---|
63 | sub is_recursive {
|
---|
64 | my $self = shift (@_);
|
---|
65 |
|
---|
66 | return 1;
|
---|
67 | }
|
---|
68 |
|
---|
69 | # return number of files processed, undef if can't process
|
---|
70 | # Note that $base_dir might be "" and that $file might
|
---|
71 | # include directories
|
---|
72 | sub read {
|
---|
73 | my $self = shift (@_);
|
---|
74 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
|
---|
75 | my $outhandle = $self->{'outhandle'};
|
---|
76 |
|
---|
77 | if ($file =~ /\.(gz|tgz|z|taz|bz|zip|tar)$/i) {
|
---|
78 |
|
---|
79 | my $filename = &util::filename_cat ($base_dir, $file);
|
---|
80 | if (!-e $filename) {
|
---|
81 | print $outhandle "ZIPPLug: WARNING: $filename does not exist\n";
|
---|
82 | return undef;
|
---|
83 | }
|
---|
84 |
|
---|
85 | my ($file_only) = $file =~ /([^\\\/]*)$/;
|
---|
86 | my $tmpdir = &util::get_tmp_filename ();
|
---|
87 | &util::mk_all_dir ($tmpdir);
|
---|
88 |
|
---|
89 | print $outhandle "ZIPPlug: extracting $file_only to $tmpdir\n";
|
---|
90 |
|
---|
91 | # save current working directory
|
---|
92 | my $cwd = cwd();
|
---|
93 | chdir ($tmpdir) || die;
|
---|
94 | &util::cp ($filename, $tmpdir);
|
---|
95 |
|
---|
96 | if ($file =~ /\.bz$/i) {
|
---|
97 | $self->bunzip ($file_only);
|
---|
98 | } elsif ($file =~ /\.zip$/i) {
|
---|
99 | $self->unzip ($file_only);
|
---|
100 | } elsif ($file =~ /\.tar$/i) {
|
---|
101 | $self->untar ($file_only);
|
---|
102 | } else {
|
---|
103 | $self->gunzip ($file_only);
|
---|
104 | }
|
---|
105 |
|
---|
106 | chdir ($cwd) || die;
|
---|
107 |
|
---|
108 | my $numdocs = &plugin::read ($pluginfo, "", $tmpdir, $metadata, $processor, $maxdocs);
|
---|
109 | &util::rm_r ($tmpdir);
|
---|
110 | return $numdocs;
|
---|
111 |
|
---|
112 | } else {
|
---|
113 | return undef;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | sub bunzip {
|
---|
118 | my $self = shift (@_);
|
---|
119 | my ($file) = @_;
|
---|
120 | system ("bunzip $file");
|
---|
121 | }
|
---|
122 |
|
---|
123 | sub unzip {
|
---|
124 | my $self = shift (@_);
|
---|
125 | my ($file) = @_;
|
---|
126 | system ("unzip $file");
|
---|
127 | &util::rm ($file) if -e $file;
|
---|
128 | }
|
---|
129 |
|
---|
130 | sub untar {
|
---|
131 | my $self = shift (@_);
|
---|
132 | my ($file) = @_;
|
---|
133 | system ("tar xf $file");
|
---|
134 | &util::rm ($file) if -e $file;
|
---|
135 | }
|
---|
136 |
|
---|
137 | sub gunzip {
|
---|
138 | my $self = shift (@_);
|
---|
139 | my ($file) = @_;
|
---|
140 | system ("gunzip $file");
|
---|
141 | }
|
---|
142 |
|
---|
143 | 1;
|
---|