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 .jar)
|
---|
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 | # ZIPPlug is currently disabled on windows as we can't expect any of the
|
---|
44 | # above utilities to be present on that OS. We should probably provide
|
---|
45 | # binaries with Greenstone some day.
|
---|
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 | sub new {
|
---|
60 | my ($class) = @_;
|
---|
61 | my $self = new BasPlug ("ZIPPlug", @_);
|
---|
62 |
|
---|
63 | return bless $self, $class;
|
---|
64 | }
|
---|
65 |
|
---|
66 | # this is a recursive plugin
|
---|
67 | sub is_recursive {
|
---|
68 | my $self = shift (@_);
|
---|
69 |
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | # return number of files processed, undef if can't process
|
---|
74 | # Note that $base_dir might be "" and that $file might
|
---|
75 | # include directories
|
---|
76 | sub read {
|
---|
77 | my $self = shift (@_);
|
---|
78 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
|
---|
79 | my $outhandle = $self->{'outhandle'};
|
---|
80 |
|
---|
81 | # disabled on windows
|
---|
82 | return undef if ($ENV{'GSDLOS'} =~ /^windows$/i);
|
---|
83 |
|
---|
84 | if ($file =~ /\.(gz|tgz|z|taz|bz|zip|jar|tar)$/i) {
|
---|
85 |
|
---|
86 | my $filename = &util::filename_cat ($base_dir, $file);
|
---|
87 | if (!-e $filename) {
|
---|
88 | print $outhandle "ZIPPLug: WARNING: $filename does not exist\n";
|
---|
89 | return undef;
|
---|
90 | }
|
---|
91 |
|
---|
92 | my ($file_only) = $file =~ /([^\\\/]*)$/;
|
---|
93 | my $tmpdir = &util::get_tmp_filename ();
|
---|
94 | &util::mk_all_dir ($tmpdir);
|
---|
95 |
|
---|
96 | print $outhandle "ZIPPlug: extracting $file_only to $tmpdir\n";
|
---|
97 |
|
---|
98 | # save current working directory
|
---|
99 | my $cwd = cwd();
|
---|
100 | chdir ($tmpdir) || die "Unable to change to $tmpdir";
|
---|
101 | &util::cp ($filename, $tmpdir);
|
---|
102 |
|
---|
103 | if ($file =~ /\.bz$/i) {
|
---|
104 | $self->bunzip ($file_only);
|
---|
105 | } elsif ($file =~ /\.(zip|jar)$/i) {
|
---|
106 | $self->unzip ($file_only);
|
---|
107 | } elsif ($file =~ /\.tar$/i) {
|
---|
108 | $self->untar ($file_only);
|
---|
109 | } else {
|
---|
110 | $self->gunzip ($file_only);
|
---|
111 | }
|
---|
112 |
|
---|
113 | chdir ($cwd) || die "Unable to change back to $cwd";
|
---|
114 |
|
---|
115 | my $numdocs = &plugin::read ($pluginfo, "", $tmpdir, $metadata, $processor, $maxdocs);
|
---|
116 | &util::rm_r ($tmpdir);
|
---|
117 | return $numdocs;
|
---|
118 |
|
---|
119 | } else {
|
---|
120 | return undef;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | sub bunzip {
|
---|
125 | my $self = shift (@_);
|
---|
126 | my ($file) = @_;
|
---|
127 | if (system ("bunzip $file")!=0)
|
---|
128 | {
|
---|
129 | &util::rm ($file);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | sub unzip {
|
---|
134 | my $self = shift (@_);
|
---|
135 | my ($file) = @_;
|
---|
136 | system ("unzip $file");
|
---|
137 | &util::rm ($file) if -e $file;
|
---|
138 | }
|
---|
139 |
|
---|
140 | sub untar {
|
---|
141 | my $self = shift (@_);
|
---|
142 | my ($file) = @_;
|
---|
143 | system ("tar xf $file");
|
---|
144 | &util::rm ($file) if -e $file;
|
---|
145 | }
|
---|
146 |
|
---|
147 | sub gunzip {
|
---|
148 | my $self = shift (@_);
|
---|
149 | my ($file) = @_;
|
---|
150 | if (system ("gunzip $file")!=0)
|
---|
151 | {
|
---|
152 | &util::rm ($file);
|
---|
153 | };
|
---|
154 | }
|
---|
155 |
|
---|
156 | 1;
|
---|