source: main/trunk/gli/src/org/greenstone/gatherer/util/UnzipTools.java

Last change on this file was 37700, checked in by anupama, 14 months ago

Implemented 2nd of Dr Bainbridge's request: GLI collection tree should have rightclick > unzip option. This context menu is now available on single or multiple selection of only zip files, but not for remote GS. UnzipTools.unzip() is no longer a void function but returns boolean, so I can test which files have been unzipped successfully and can be deleted thereafter. Decompressed files were showing by calling refresh on the collection tree, but I couldn't initially get the deleted source zip files to disappear. And if I didn't call refresh, the deletes would show (but not the decompressed files). I couldn't work out what caused the tree to refresh on delete context menu or rubbish bin click, nor where (if anywhere) an event was fired to indicate deletion had completed and caused a refresh. Instead, I found that a commented out call to g_man.refreshCollectionTree in FileQueue::run() did the job of showing both decompressed files and not showing the deleted source zip files. The question remains: why was the refreshCollectionTree() call in FileQueue::run() commented out in the first place? It fixes things for my work here, but will it break something somewhere else?

  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 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.*;
33
34
35public class UnzipTools
36{
37 static public boolean unzipFile(String zip_file_path, String base_directory_path)
38 {
39 boolean success = true;
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 success = false;
55 }
56 }
57
58 // File case
59 else {
60 // Write out file to disk
61
62 // Make sure its parent directory exists.
63 File dir = new File(zip_entry_file.getParent());
64 dir.mkdirs();
65
66 // Don't need to unzip files called empty which were created during the zipping process
67 // in order to be able to generate empty directories in a zip (see remote.ZipTools)
68 if(!zip_entry_file.getName().equals("empty")) {
69 // Set up input stream
70 InputStream zis = zip_file.getInputStream(zip_entry);
71 BufferedInputStream bzis = new BufferedInputStream(zis);
72 DataInputStream dbzis = new DataInputStream(bzis);
73
74 // Set up output stream
75 FileOutputStream fzos = new FileOutputStream(zip_entry_file);
76 BufferedOutputStream bfzos = new BufferedOutputStream(fzos);
77
78 byte[] buf = new byte[1024];
79 int len;
80 while ((len = dbzis.read(buf)) >= 0) {
81 bfzos.write(buf,0,len);
82 }
83
84 dbzis.close();
85 bzis.close();
86 zis.close();
87
88 bfzos.close();
89 fzos.close();
90 }
91 }
92 }
93
94 zip_file.close();
95 }
96 catch (Exception exception) {
97 exception.printStackTrace();
98 success = false;
99 }
100
101 return success;
102 }
103}
Note: See TracBrowser for help on using the repository browser.