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 | # bzip2 (.bz2)
|
---|
33 | # zip (.zip .jar)
|
---|
34 | # tar (.tar)
|
---|
35 | #
|
---|
36 | # this plugin relies on the following utilities being present
|
---|
37 | # (if trying to process the corresponding formats)
|
---|
38 | #
|
---|
39 | # gunzip (for gzip)
|
---|
40 | # bunzip (for bzip)
|
---|
41 | # bunzip2
|
---|
42 | # unzip (for zip)
|
---|
43 | # tar (for tar)
|
---|
44 |
|
---|
45 | # 12/05/02 Added usage datastructure - John Thompson
|
---|
46 |
|
---|
47 | package ZIPPlug;
|
---|
48 |
|
---|
49 | use BasPlug;
|
---|
50 | use plugin;
|
---|
51 | use util;
|
---|
52 | use Cwd;
|
---|
53 |
|
---|
54 |
|
---|
55 | BEGIN {
|
---|
56 | @ISA = ('BasPlug');
|
---|
57 | }
|
---|
58 |
|
---|
59 | my $options = { 'name' => "ZIPPlug",
|
---|
60 | 'desc' => "{ZIPPlug.desc}",
|
---|
61 | 'abstract' => "no",
|
---|
62 | 'inherits' => "yes" };
|
---|
63 |
|
---|
64 | sub new {
|
---|
65 | my ($class) = @_;
|
---|
66 | my $self = new BasPlug ("ZIPPlug", @_);
|
---|
67 |
|
---|
68 | # 14-05-02 To allow for proper inheritance of arguments - John Thompson
|
---|
69 | my $option_list = $self->{'option_list'};
|
---|
70 | push( @{$option_list}, $options );
|
---|
71 |
|
---|
72 | return bless $self, $class;
|
---|
73 | }
|
---|
74 |
|
---|
75 | # this is a recursive plugin
|
---|
76 | sub is_recursive {
|
---|
77 | my $self = shift (@_);
|
---|
78 |
|
---|
79 | return 1;
|
---|
80 | }
|
---|
81 |
|
---|
82 | # return number of files processed, undef if can't process
|
---|
83 | # Note that $base_dir might be "" and that $file might
|
---|
84 | # include directories
|
---|
85 | sub read {
|
---|
86 | my $self = shift (@_);
|
---|
87 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
|
---|
88 | my $outhandle = $self->{'outhandle'};
|
---|
89 |
|
---|
90 | if ($file =~ /\.(gz|tgz|z|taz|bz|bz2|zip|jar|tar)$/i) {
|
---|
91 |
|
---|
92 | my $filename = $file;
|
---|
93 | $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
|
---|
94 | if (!-e $filename) {
|
---|
95 | print $outhandle "ZIPPLug: WARNING: $filename does not exist\n";
|
---|
96 | return undef;
|
---|
97 | }
|
---|
98 |
|
---|
99 | my ($file_only) = $file =~ /([^\\\/]*)$/;
|
---|
100 | my $tmpdir = &util::get_tmp_filename ();
|
---|
101 | &util::mk_all_dir ($tmpdir);
|
---|
102 |
|
---|
103 | print $outhandle "ZIPPlug: extracting $file_only to $tmpdir\n";
|
---|
104 |
|
---|
105 | # save current working directory
|
---|
106 | my $cwd = cwd();
|
---|
107 | chdir ($tmpdir) || die "Unable to change to $tmpdir";
|
---|
108 | &util::cp ($filename, $tmpdir);
|
---|
109 |
|
---|
110 | if ($file =~ /\.bz$/i) {
|
---|
111 | $self->bunzip ($file_only);
|
---|
112 | } elsif ($file =~ /\.bz2$/i) {
|
---|
113 | $self->bunzip2 ($file_only);
|
---|
114 | } elsif ($file =~ /\.(zip|jar)$/i) {
|
---|
115 | $self->unzip ($file_only);
|
---|
116 | } elsif ($file =~ /\.tar$/i) {
|
---|
117 | $self->untar ($file_only);
|
---|
118 | } else {
|
---|
119 | $self->gunzip ($file_only);
|
---|
120 | }
|
---|
121 |
|
---|
122 | chdir ($cwd) || die "Unable to change back to $cwd";
|
---|
123 |
|
---|
124 | my $numdocs = &plugin::read ($pluginfo, "", $tmpdir, $metadata, $processor, $maxdocs);
|
---|
125 | &util::rm_r ($tmpdir);
|
---|
126 |
|
---|
127 | $self->{'num_archives'} ++;
|
---|
128 |
|
---|
129 | return $numdocs;
|
---|
130 |
|
---|
131 | } else {
|
---|
132 | return undef;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | sub bunzip {
|
---|
137 | my $self = shift (@_);
|
---|
138 | my ($file) = @_;
|
---|
139 |
|
---|
140 | if (system ("bunzip $file")!=0)
|
---|
141 | {
|
---|
142 | &util::rm ($file);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | sub bunzip2 {
|
---|
147 | my $self = shift (@_);
|
---|
148 | my ($file) = @_;
|
---|
149 |
|
---|
150 | if (system ("bunzip2 $file")!=0)
|
---|
151 | {
|
---|
152 | &util::rm ($file);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | sub unzip {
|
---|
157 | my $self = shift (@_);
|
---|
158 | my ($file) = @_;
|
---|
159 |
|
---|
160 | system ("unzip $file");
|
---|
161 | &util::rm ($file) if -e $file;
|
---|
162 | }
|
---|
163 |
|
---|
164 | sub untar {
|
---|
165 | my $self = shift (@_);
|
---|
166 | my ($file) = @_;
|
---|
167 |
|
---|
168 | system ("tar xf $file");
|
---|
169 | &util::rm ($file) if -e $file;
|
---|
170 | }
|
---|
171 |
|
---|
172 | sub gunzip {
|
---|
173 | my $self = shift (@_);
|
---|
174 | my ($file) = @_;
|
---|
175 |
|
---|
176 | if (system ("gunzip $file")!=0)
|
---|
177 | {
|
---|
178 | &util::rm ($file);
|
---|
179 | };
|
---|
180 | }
|
---|
181 |
|
---|
182 | 1;
|
---|