source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/remote/ZipTools.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 4.8 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.remote;
28
29
30import java.io.*;
31import java.util.zip.*;
32
33
34public 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 // empty directory. Java zips don't allow empty dirs, so need to put some dummy file in there
80 if(child_files.length == 0) {
81 String rel_path = relative_file_path.replace('\\', '/') + File.separator + "empty";
82 System.err.println("Zipping empty dir requires dummy file: " + rel_path);
83 zos.putNextEntry(new ZipEntry(rel_path));
84 }
85 }
86
87 // File case
88 else {
89 // Add a zip entry for this file
90 if (zip_filter.shouldIncludeFile(relative_file_path)) {
91 // Always use Unix style paths in zip files, even on Windows
92 zos.putNextEntry(new ZipEntry(relative_file_path.replace('\\', '/')));
93
94 if (zip_filter.shouldIncludeFileContent(relative_file_path)) {
95 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
96 zip_filter.filterFileContent(relative_file_path, bis, zos);
97 bis.close();
98 }
99 }
100 }
101 }
102 catch (Exception exception) {
103 exception.printStackTrace();
104 }
105 }
106
107
108 public interface ZipFilter
109 {
110 public boolean shouldIncludeFile(String relative_file_path);
111
112 public boolean shouldIncludeFileContent(String relative_file_path);
113
114 public void filterFileContent(String relative_file_path, BufferedInputStream bis, ZipOutputStream zos);
115 }
116
117
118 static public class NullZipFilter
119 implements ZipFilter
120 {
121 public boolean shouldIncludeFile(String relative_file_path)
122 {
123 // All files are included
124 return true;
125 }
126
127
128 public boolean shouldIncludeFileContent(String relative_file_path)
129 {
130 // Content for all files is included
131 return true;
132 }
133
134
135 public void filterFileContent(String relative_file_path, BufferedInputStream bis, ZipOutputStream zos)
136 {
137 // No filtering: just read the file and write it directly out to zip
138 try {
139 byte[] data = new byte[1024];
140 int bytes_read;
141 while ((bytes_read = bis.read(data, 0, 1024)) > -1) {
142 zos.write(data, 0, bytes_read);
143 }
144 }
145 catch (Exception exception) {
146 // We can't use DebugStream here
147 exception.printStackTrace();
148 }
149 }
150 }
151}
Note: See TracBrowser for help on using the repository browser.