1 | /**
|
---|
2 | *############################################################################
|
---|
3 | * A component of the Greenstone Librarian Interface, part of the Greenstone
|
---|
4 | * digital library suite from the New Zealand Digital Library Project at the
|
---|
5 | * University of Waikato, New Zealand.
|
---|
6 | *
|
---|
7 | * Author: David Bainbridge, NZDL Project, University of Waikato, NZ
|
---|
8 | *
|
---|
9 | * Copyright (C) 2005 New Zealand Digital Library Project
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | *############################################################################
|
---|
25 | */
|
---|
26 |
|
---|
27 | package org.greenstone.gatherer.remote;
|
---|
28 |
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.util.zip.*;
|
---|
32 |
|
---|
33 |
|
---|
34 | public class ZipTools
|
---|
35 | {
|
---|
36 | static public void zipFiles(String zip_file_path, String base_directory_path, String[] relative_file_paths)
|
---|
37 | {
|
---|
38 | try {
|
---|
39 | ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip_file_path));
|
---|
40 | ZipFilter null_zip_filter = new NullZipFilter();
|
---|
41 |
|
---|
42 | // Add each file/directory in turn to the zip file
|
---|
43 | for (int i = 0; i < relative_file_paths.length; i++) {
|
---|
44 | String relative_file_path = relative_file_paths[i];
|
---|
45 | addFileToZip(zos, base_directory_path, relative_file_path, null_zip_filter);
|
---|
46 | }
|
---|
47 |
|
---|
48 | zos.close();
|
---|
49 | }
|
---|
50 | catch (Exception exception) {
|
---|
51 | exception.printStackTrace();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | static public void addFileToZip(ZipOutputStream zos, String base_directory_path, String relative_file_path, ZipFilter zip_filter)
|
---|
57 | {
|
---|
58 | File file = new File(base_directory_path + File.separator + relative_file_path);
|
---|
59 |
|
---|
60 | // Check that the file/directory exists
|
---|
61 | if (!file.exists()) {
|
---|
62 | System.err.println("File " + file + " does not exist!");
|
---|
63 | return;
|
---|
64 | }
|
---|
65 |
|
---|
66 | try {
|
---|
67 | // Directory case
|
---|
68 | if (file.isDirectory()) {
|
---|
69 | // Add a zip entry for this directory
|
---|
70 | // Actually, don't -- this messes things up completely on Windows and doesn't seem to matter on Linux
|
---|
71 | // zos.putNextEntry(new ZipEntry(relative_file_path + File.separator));
|
---|
72 |
|
---|
73 | // Apply recursively to each of the children of the directory
|
---|
74 | File[] child_files = file.listFiles();
|
---|
75 | for (int i = 0; i < child_files.length; i++) {
|
---|
76 | addFileToZip(zos, base_directory_path, relative_file_path + File.separator + child_files[i].getName(), zip_filter);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | // File case
|
---|
81 | else {
|
---|
82 | // Add a zip entry for this file
|
---|
83 | if (zip_filter.shouldIncludeFile(relative_file_path)) {
|
---|
84 | // Always use Unix style paths in zip files, even on Windows
|
---|
85 | zos.putNextEntry(new ZipEntry(relative_file_path.replace('\\', '/')));
|
---|
86 |
|
---|
87 | if (zip_filter.shouldIncludeFileContent(relative_file_path)) {
|
---|
88 | BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
---|
89 | zip_filter.filterFileContent(relative_file_path, bis, zos);
|
---|
90 | bis.close();
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | catch (Exception exception) {
|
---|
96 | exception.printStackTrace();
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | public interface ZipFilter
|
---|
102 | {
|
---|
103 | public boolean shouldIncludeFile(String relative_file_path);
|
---|
104 |
|
---|
105 | public boolean shouldIncludeFileContent(String relative_file_path);
|
---|
106 |
|
---|
107 | public void filterFileContent(String relative_file_path, BufferedInputStream bis, ZipOutputStream zos);
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | static public class NullZipFilter
|
---|
112 | implements ZipFilter
|
---|
113 | {
|
---|
114 | public boolean shouldIncludeFile(String relative_file_path)
|
---|
115 | {
|
---|
116 | // All files are included
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | public boolean shouldIncludeFileContent(String relative_file_path)
|
---|
122 | {
|
---|
123 | // Content for all files is included
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | public void filterFileContent(String relative_file_path, BufferedInputStream bis, ZipOutputStream zos)
|
---|
129 | {
|
---|
130 | // No filtering: just read the file and write it directly out to zip
|
---|
131 | try {
|
---|
132 | byte[] data = new byte[1024];
|
---|
133 | int bytes_read;
|
---|
134 | while ((bytes_read = bis.read(data, 0, 1024)) > -1) {
|
---|
135 | zos.write(data, 0, bytes_read);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | catch (Exception exception) {
|
---|
139 | // We can't use DebugStream here
|
---|
140 | exception.printStackTrace();
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|