source: trunk/gli/src/org/greenstone/gatherer/util/ZipTools.java@ 12730

Last change on this file since 12730 was 10726, checked in by mdewsnip, 19 years ago

(MAJOR CHANGE) This is the remote Greenstone building functionality implemented for the West Yorkshire Fire and Rescue Service. It allows collections to be built without having a local version of Greenstone, using either a stand-alone version of the GLI or the applet.

The collections are stored on the server (allowing people to collaborate on collections -- but not at the same time), and only what is necessary to let the user edit the collection is downloaded. Any changes to the collection are uploaded to the server.

An access restriction mechanism is implemented which uses the standard Greenstone user database to control who has access to collections.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
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
27package org.greenstone.gatherer.util;
28
29
30import java.io.*;
31import java.util.*;
32import java.util.zip.*;
33import org.w3c.dom.*;
34
35
36public class ZipTools
37{
38 static public void unzipFile(String zip_file_path, String base_directory_path)
39 {
40 try {
41 ZipFile zip_file = new ZipFile(new File(zip_file_path), ZipFile.OPEN_READ);
42
43 Enumeration e = zip_file.entries();
44 while (e.hasMoreElements()) {
45 ZipEntry zip_entry = (ZipEntry) e.nextElement();
46 File zip_entry_file = new File(base_directory_path + zip_entry.getName());
47 // System.err.println(" Unzipping: " + zip_entry_file.getAbsolutePath());
48
49 // Directory case
50 if (zip_entry.isDirectory()) {
51 // Create named directory, if it doesn't already exist
52 if (!zip_entry_file.exists() && !zip_entry_file.mkdirs()) {
53 System.err.println("Error: unable to create directory " + zip_entry_file);
54 }
55 }
56
57 // File case
58 else {
59 // Write out file to disk
60
61 // Make sure its parent directory exists.
62 File dir = new File(zip_entry_file.getParent());
63 dir.mkdirs();
64
65 // Set up input stream
66 InputStream zis = zip_file.getInputStream(zip_entry);
67 BufferedInputStream bzis = new BufferedInputStream(zis);
68 DataInputStream dbzis = new DataInputStream(bzis);
69
70 // Set up output stream
71 FileOutputStream fzos = new FileOutputStream(zip_entry_file);
72 BufferedOutputStream bfzos = new BufferedOutputStream(fzos);
73
74 byte[] buf = new byte[1024];
75 int len;
76 while ((len = dbzis.read(buf)) >= 0) {
77 bfzos.write(buf,0,len);
78 }
79
80 dbzis.close();
81 bzis.close();
82 zis.close();
83
84 bfzos.close();
85 fzos.close();
86 }
87 }
88
89 zip_file.close();
90 }
91 catch (Exception exception) {
92 exception.printStackTrace();
93 }
94 }
95
96
97 static public void zipFiles(String zip_file_path, String base_directory_path, String[] relative_file_paths)
98 {
99 try {
100 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip_file_path));
101 ZipFilter null_zip_filter = new NullZipFilter();
102
103 // Add each file/directory in turn to the zip file
104 for (int i = 0; i < relative_file_paths.length; i++) {
105 String relative_file_path = relative_file_paths[i];
106 addFileToZip(zos, base_directory_path, relative_file_path, null_zip_filter);
107 }
108
109 zos.close();
110 }
111 catch (Exception exception) {
112 exception.printStackTrace();
113 }
114 }
115
116
117 static public void addFileToZip(ZipOutputStream zos, String base_directory_path, String relative_file_path, ZipFilter zip_filter)
118 {
119 File file = new File(base_directory_path + File.separator + relative_file_path);
120
121 // Check that the file/directory exists
122 if (!file.exists()) {
123 System.err.println("File " + file + " does not exist!");
124 return;
125 }
126
127 try {
128 // Directory case
129 if (file.isDirectory()) {
130 // Add a zip entry for this directory
131 // Actually, don't -- this messes things up completely on Windows and doesn't seem to matter on Linux
132 // zos.putNextEntry(new ZipEntry(relative_file_path + File.separator));
133
134 // Apply recursively to each of the children of the directory
135 File[] child_files = file.listFiles();
136 for (int i = 0; i < child_files.length; i++) {
137 addFileToZip(zos, base_directory_path, relative_file_path + File.separator + child_files[i].getName(), zip_filter);
138 }
139 }
140
141 // File case
142 else {
143 // Add a zip entry for this file
144 if (zip_filter.shouldIncludeFile(relative_file_path)) {
145 // Always use Unix style paths in zip files, even on Windows
146 zos.putNextEntry(new ZipEntry(relative_file_path.replace('\\', '/')));
147
148 if (zip_filter.shouldIncludeFileContent(relative_file_path)) {
149 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
150 byte[] data = new byte[1024];
151 int bytes_read;
152 while ((bytes_read = zip_filter.filterFileContent(bis, data, 1024)) > -1) {
153 zos.write(data, 0, bytes_read);
154 }
155 bis.close();
156 }
157 }
158 }
159 }
160 catch (Exception exception) {
161 exception.printStackTrace();
162 }
163 }
164
165
166 public interface ZipFilter
167 {
168 public boolean shouldIncludeFile(String relative_file_path);
169
170 public boolean shouldIncludeFileContent(String relative_file_path);
171
172 public int filterFileContent(BufferedInputStream bis, byte[] data, int bytes_to_read);
173 }
174
175
176 static public class NullZipFilter
177 implements ZipFilter
178 {
179 public boolean shouldIncludeFile(String relative_file_path)
180 {
181 // All files are included
182 return true;
183 }
184
185
186 public boolean shouldIncludeFileContent(String relative_file_path)
187 {
188 // Content for all files is included
189 return true;
190 }
191
192
193 public int filterFileContent(BufferedInputStream bis, byte[] data, int bytes_to_read)
194 {
195 try {
196 return bis.read(data, 0, bytes_to_read);
197 }
198 catch (Exception exception) {
199 exception.printStackTrace();
200 }
201
202 return -1;
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.