Changeset 10267
- Timestamp:
- 2005-07-22T16:40:13+12:00 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/util/ZipTools.java
r10250 r10267 38 38 public class ZipTools 39 39 { 40 static protected String unixStylePath(String path) 40 static public void unzipFile(String zip_file_path, String base_directory_path) 41 { 42 try { 43 ZipFile zip_file = new ZipFile(new File(zip_file_path), ZipFile.OPEN_READ); 44 45 Enumeration e = zip_file.entries(); 46 while (e.hasMoreElements()) { 47 ZipEntry zip_entry = (ZipEntry) e.nextElement(); 48 File zip_entry_file = new File(base_directory_path + zip_entry.getName()); 49 DebugStream.println(" Unzipping: " + zip_entry_file.getAbsolutePath()); 50 51 // Directory case 52 if (zip_entry.isDirectory()) { 53 // Create named directory, if it doesn't already exist 54 if (!zip_entry_file.exists() && !zip_entry_file.mkdirs()) { 55 System.err.println("Error: unable to create directory " + zip_entry_file); 56 } 57 } 58 59 // File case 60 else { 61 // Write out file to disk 62 63 // Make sure it's parent directory exists. 64 File dir = new File(zip_entry_file.getParent()); 65 dir.mkdirs(); 66 67 // Set up input stream 68 InputStream zis = zip_file.getInputStream(zip_entry); 69 BufferedInputStream bzis = new BufferedInputStream(zis); 70 DataInputStream dbzis = new DataInputStream(bzis); 71 72 // Set up output stream 73 FileOutputStream fzos = new FileOutputStream(zip_entry_file); 74 BufferedOutputStream bfzos = new BufferedOutputStream(fzos); 75 76 byte[] buf = new byte[1024]; 77 int len; 78 while ((len = dbzis.read(buf)) >= 0) { 79 bfzos.write(buf,0,len); 80 } 81 82 dbzis.close(); 83 bzis.close(); 84 zis.close(); 85 86 bfzos.close(); 87 fzos.close(); 88 } 89 } 90 91 zip_file.close(); 92 } 93 catch (Exception exception) { 94 DebugStream.printStackTrace(exception); 95 } 96 } 97 98 99 static public void zipFiles(String zip_file_path, String base_directory_path, String[] relative_file_paths) 100 { 101 try { 102 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip_file_path)); 103 ZipFilter null_zip_filter = new NullZipFilter(); 104 105 // Add each file/directory in turn to the zip file 106 for (int i = 0; i < relative_file_paths.length; i++) { 107 String relative_file_path = relative_file_paths[i]; 108 addFileToZip(zos, base_directory_path, relative_file_path, null_zip_filter); 109 } 110 111 zos.close(); 112 } 113 catch (Exception exception) { 114 DebugStream.printStackTrace(exception); 115 } 116 } 117 118 119 static public void addFileToZip(ZipOutputStream zos, String base_directory_path, String relative_file_path, ZipFilter zip_filter) 120 { 121 File file = new File(base_directory_path + File.separator + relative_file_path); 122 123 // Check that the file/directory exists 124 if (!file.exists()) { 125 System.err.println("File " + file + " does not exist!"); 126 return; 127 } 128 129 try { 130 // Directory case 131 if (file.isDirectory()) { 132 // Add a zip entry for this directory 133 zos.putNextEntry(new ZipEntry(relative_file_path + File.separator)); 134 135 // Apply recursively to each of the children of the directory 136 File[] child_files = file.listFiles(); 137 for (int i = 0; i < child_files.length; i++) { 138 addFileToZip(zos, base_directory_path, relative_file_path + File.separator + child_files[i].getName(), zip_filter); 139 } 140 } 141 142 // File case 143 else { 144 // Add a zip entry for this file 145 if (zip_filter.shouldIncludeFile(relative_file_path)) { 146 zos.putNextEntry(new ZipEntry(relative_file_path)); 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 = bis.read(data, 0, 1024)) > -1) { 153 zos.write(data, 0, bytes_read); 154 } 155 bis.close(); 156 } 157 } 158 } 159 } 160 catch (Exception exception) { 161 DebugStream.printStackTrace(exception); 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 173 174 static public class NullZipFilter 175 implements ZipFilter 176 { 177 public boolean shouldIncludeFile(String relative_file_path) 178 { 179 // All files are included 180 return true; 181 } 182 183 184 public boolean shouldIncludeFileContent(String relative_file_path) 185 { 186 // Content for all files is included 187 return true; 188 } 189 } 190 191 192 // ---------------------------------------------------------------------------------------------------- 193 194 195 static private String unixStylePath(String path) 41 196 { 42 197 String unix_path = path.replace('\\','/'); … … 61 216 * @see zipup(String, String, String, GShell, String, String) 62 217 */ 63 static pr otectedboolean zipFunc(ZipOutputStream zos, String file_path, int prefix_strip, GShell source, boolean encountered_file, String accept_expr, String reject_expr)218 static private boolean zipFunc(ZipOutputStream zos, String file_path, int prefix_strip, GShell source, boolean encountered_file, String accept_expr, String reject_expr) 64 219 { 65 220 String new_file_path = file_path; … … 158 313 * @see zipFunc(ZipOutputStream, String, int, GShell, boolean, String, String) 159 314 */ 160 static pr otectedboolean dirFunc (ZipOutputStream zos, String dir_name, int prefix_strip, GShell source, boolean encountered_file, String accept_expr, String reject_expr)315 static private boolean dirFunc (ZipOutputStream zos, String dir_name, int prefix_strip, GShell source, boolean encountered_file, String accept_expr, String reject_expr) 161 316 { 162 317 File dirObj = new File(dir_name); … … 278 433 return encountered_file; 279 434 } 280 435 436 281 437 /** 282 438 * Method to unzip a zip file associated with a particular collection.
Note:
See TracChangeset
for help on using the changeset viewer.