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, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 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.util;
|
---|
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 | public static final void main(String[] args)
|
---|
62 | {
|
---|
63 | Enumeration entries;
|
---|
64 | ZipFile zipFile;
|
---|
65 |
|
---|
66 | if(args.length != 2) {
|
---|
67 | System.err.println("Usage: Unzip gsdlhome zipfile");
|
---|
68 | return;
|
---|
69 | }
|
---|
70 |
|
---|
71 | String gsdl_home = args[0];
|
---|
72 | String col_dir = gsdl_home + "collect" + File.separator;
|
---|
73 |
|
---|
74 | try {
|
---|
75 | zipFile = new ZipFile(args[1]);
|
---|
76 |
|
---|
77 | entries = zipFile.entries();
|
---|
78 |
|
---|
79 | while(entries.hasMoreElements()) {
|
---|
80 | ZipEntry entry = (ZipEntry)entries.nextElement();
|
---|
81 |
|
---|
82 | if(entry.isDirectory()) {
|
---|
83 | // Assume directories are stored parents first then children.
|
---|
84 | // System.err.println("Extracting directory: " + entry.getName());
|
---|
85 | // This is not robust, just for demonstration purposes.
|
---|
86 | String dir_name = entry.getName();
|
---|
87 | String full_dir_name = col_dir + dir_name;
|
---|
88 | File dirFile = new File(full_dir_name);
|
---|
89 | dirFile.mkdir();
|
---|
90 | }
|
---|
91 | else {
|
---|
92 | // System.err.println("Extracting file: " + entry.getName());
|
---|
93 | String file_name = entry.getName();
|
---|
94 | String full_file_name = col_dir + file_name;
|
---|
95 | FileOutputStream fos = new FileOutputStream(full_file_name);
|
---|
96 | BufferedOutputStream bos = new BufferedOutputStream(fos);
|
---|
97 |
|
---|
98 | copyInputStream(zipFile.getInputStream(entry),bos);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | zipFile.close();
|
---|
103 | }
|
---|
104 | catch (IOException ioe) {
|
---|
105 | System.err.println("Unhandled exception:");
|
---|
106 | ioe.printStackTrace();
|
---|
107 | return;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|