Source code: jsd/ftp/jar/JarExtractor.java
1 /*
2 * ----------------------------------------------------------------------------
3 * JStrangeDownloader and all accompanying source code files are
4 * Copyright (C) 2002 Dusty Davidson (dustyd@iastate.edu)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * ----------------------------------------------------------------------------
20 *
21 * Please see gpl.txt for the full text of the GNU General Public
22 * License.
23 */
24
25 package jsd.ftp.jar;
26
27 import java.io.File;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.io.FileOutputStream;
31 import java.util.Enumeration;
32 import java.util.zip.ZipFile;
33 import java.util.zip.ZipEntry;
34 import java.util.jar.JarFile;
35
36
37 /**
38 * This is JAR file extractor class. It extracts the JAR file
39 * in a separate thread. Where we are passing a MyJarObserver
40 * object to track the current status of this decompression.
41 *
42 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
43 */
44 public
45 abstract class JarExtractor implements Runnable {
46
47 protected File mDir;
48 protected File mJarFile;
49 protected JarObserverContainer mObserverCont;
50 protected boolean mbIsPauseRequest = false;
51 protected boolean mbIsStopRequest = false;
52
53
54 public JarExtractor(File jarFile, File dir) {
55 mDir = dir;
56 mJarFile = jarFile;
57 mObserverCont = new JarObserverContainer();
58 }
59
60 /**
61 * add observer
62 */
63 public void addObserver(JarObserver obsr) {
64 mObserverCont.addObserver(obsr);
65 }
66
67
68 /**
69 * remove observer
70 */
71 public void removeObserver(JarObserver obsr) {
72 mObserverCont.removeObserver(obsr);
73 }
74
75
76 /**
77 * Extract an entry from the zip file.
78 */
79 public void extract(ZipFile jf, ZipEntry ze) throws Exception {
80 File fl = new File(mDir, ze.toString());
81
82 // directory create it
83 if (ze.isDirectory()) {
84 fl.mkdirs();
85 return;
86 }
87
88 File par = new File(fl.getParent());
89 if (!par.exists()) {
90 par.mkdirs();
91 }
92
93 // file decompres it
94 FileOutputStream fos = new FileOutputStream(fl);
95 extract(jf, ze, fos);
96 fos.close();
97 }
98
99
100 /**
101 * write ZipEntry into OutputStream
102 */
103 public static void extract (ZipFile jf, ZipEntry ze, OutputStream out)
104 throws Exception {
105
106 InputStream is = jf.getInputStream(ze);
107 byte buff[] = new byte[1024];
108 int cnt = -1;
109 while ((cnt = is.read(buff)) != -1) {
110 out.write(buff, 0, cnt);
111 }
112 is.close();
113 }
114
115
116
117 /**
118 * stop decompression
119 */
120 public void stop() {
121 mbIsStopRequest = true;
122 }
123
124 public boolean isStopped() {
125 return mbIsStopRequest;
126 }
127
128 /**
129 * pause decompression
130 */
131 public void pause() {
132 mbIsPauseRequest = true;
133 }
134
135 public boolean isPaused() {
136 return mbIsPauseRequest;
137 }
138
139 /**
140 * resume decompression
141 */
142 public void resume() {
143 mbIsPauseRequest = false;
144 }
145
146 /**
147 * get jar file name
148 */
149 public String toString() {
150 return mJarFile.getAbsolutePath();
151 }
152
153
154 /**
155 * start decompression
156 */
157 public abstract void extract();
158
159 }