1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: David Bainbridge, NZDL Project, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 2005 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 |
|
---|
38 | package org.greenstone.gatherer.remote;
|
---|
39 |
|
---|
40 | import java.io.*;
|
---|
41 | import java.util.*;
|
---|
42 | import java.util.zip.*;
|
---|
43 |
|
---|
44 |
|
---|
45 | public class Unzip
|
---|
46 | {
|
---|
47 |
|
---|
48 | public static final void copyInputStream(InputStream in, OutputStream out)
|
---|
49 | throws IOException
|
---|
50 | {
|
---|
51 | byte[] buffer = new byte[1024];
|
---|
52 | int len;
|
---|
53 |
|
---|
54 | while((len = in.read(buffer)) >= 0)
|
---|
55 | out.write(buffer, 0, len);
|
---|
56 |
|
---|
57 | in.close();
|
---|
58 | out.close();
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | static public void main(String[] args)
|
---|
63 | {
|
---|
64 | if (args.length != 2) {
|
---|
65 | System.err.println("Usage: Unzip <zip-file> <destination-directory>");
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | String zip_file_path = args[0];
|
---|
70 | // System.err.println("Zip file path: " + zip_file_path);
|
---|
71 | String destination_directory_path = args[1];
|
---|
72 | if (!destination_directory_path.endsWith(File.separator)) {
|
---|
73 | destination_directory_path += File.separator;
|
---|
74 | }
|
---|
75 | // System.err.println("Destination directory path: " + destination_directory_path);
|
---|
76 |
|
---|
77 | try {
|
---|
78 | ZipFile zipFile = new ZipFile(zip_file_path);
|
---|
79 |
|
---|
80 | Enumeration entries = zipFile.entries();
|
---|
81 | while (entries.hasMoreElements()) {
|
---|
82 | ZipEntry entry = (ZipEntry) entries.nextElement();
|
---|
83 |
|
---|
84 | if (entry.isDirectory()) {
|
---|
85 | // Assume directories are stored parents first then children.
|
---|
86 | // System.err.println("Extracting directory: " + entry.getName());
|
---|
87 | // This is not robust, just for demonstration purposes.
|
---|
88 | String dir_name = entry.getName();
|
---|
89 | String full_dir_name = destination_directory_path + dir_name;
|
---|
90 | // System.err.println("Full directory name: " + full_dir_name);
|
---|
91 | File dirFile = new File(full_dir_name);
|
---|
92 | dirFile.mkdir();
|
---|
93 | }
|
---|
94 | else {
|
---|
95 | // System.err.println("Extracting file: " + entry.getName());
|
---|
96 | String file_name = entry.getName();
|
---|
97 | String full_file_name = destination_directory_path + file_name;
|
---|
98 | File directory = (new File(full_file_name)).getParentFile();
|
---|
99 | // System.err.println("Full file name: " + full_file_name);
|
---|
100 | if (!directory.exists() && !directory.mkdirs()) {
|
---|
101 | System.err.println("Error: Could not create directory " + directory + "!");
|
---|
102 | }
|
---|
103 |
|
---|
104 | FileOutputStream fos = new FileOutputStream(full_file_name);
|
---|
105 | BufferedOutputStream bos = new BufferedOutputStream(fos);
|
---|
106 |
|
---|
107 | copyInputStream(zipFile.getInputStream(entry),bos);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | zipFile.close();
|
---|
112 | }
|
---|
113 | catch (Exception exception) {
|
---|
114 | System.out.println("ERROR: " + exception);
|
---|
115 | exception.printStackTrace();
|
---|
116 | System.exit(1);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|