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.util;
|
---|
28 |
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.util.*;
|
---|
32 | import java.util.zip.*;
|
---|
33 |
|
---|
34 |
|
---|
35 | public class UnzipTools
|
---|
36 | {
|
---|
37 | static public void unzipFile(String zip_file_path, String base_directory_path)
|
---|
38 | {
|
---|
39 | try {
|
---|
40 | ZipFile zip_file = new ZipFile(new File(zip_file_path), ZipFile.OPEN_READ);
|
---|
41 |
|
---|
42 | Enumeration e = zip_file.entries();
|
---|
43 | while (e.hasMoreElements()) {
|
---|
44 | ZipEntry zip_entry = (ZipEntry) e.nextElement();
|
---|
45 | File zip_entry_file = new File(base_directory_path + zip_entry.getName());
|
---|
46 | // System.err.println(" Unzipping: " + zip_entry_file.getAbsolutePath());
|
---|
47 |
|
---|
48 | // Directory case
|
---|
49 | if (zip_entry.isDirectory()) {
|
---|
50 | // Create named directory, if it doesn't already exist
|
---|
51 | if (!zip_entry_file.exists() && !zip_entry_file.mkdirs()) {
|
---|
52 | System.err.println("Error: unable to create directory " + zip_entry_file);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | // File case
|
---|
57 | else {
|
---|
58 | // Write out file to disk
|
---|
59 |
|
---|
60 | // Make sure its parent directory exists.
|
---|
61 | File dir = new File(zip_entry_file.getParent());
|
---|
62 | dir.mkdirs();
|
---|
63 |
|
---|
64 | // Set up input stream
|
---|
65 | InputStream zis = zip_file.getInputStream(zip_entry);
|
---|
66 | BufferedInputStream bzis = new BufferedInputStream(zis);
|
---|
67 | DataInputStream dbzis = new DataInputStream(bzis);
|
---|
68 |
|
---|
69 | // Set up output stream
|
---|
70 | FileOutputStream fzos = new FileOutputStream(zip_entry_file);
|
---|
71 | BufferedOutputStream bfzos = new BufferedOutputStream(fzos);
|
---|
72 |
|
---|
73 | byte[] buf = new byte[1024];
|
---|
74 | int len;
|
---|
75 | while ((len = dbzis.read(buf)) >= 0) {
|
---|
76 | bfzos.write(buf,0,len);
|
---|
77 | }
|
---|
78 |
|
---|
79 | dbzis.close();
|
---|
80 | bzis.close();
|
---|
81 | zis.close();
|
---|
82 |
|
---|
83 | bfzos.close();
|
---|
84 | fzos.close();
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | zip_file.close();
|
---|
89 | }
|
---|
90 | catch (Exception exception) {
|
---|
91 | exception.printStackTrace();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|