source: AE/installer2/src/net/oni2/aeinstaller/backend/unpack/Unpacker.java@ 605

Last change on this file since 605 was 605, checked in by alloc, 12 years ago

AEI2:

  • Added mod download prior to installation
  • Dependency checking (needs verification)
  • Conflicts checking basis (not implemented)
  • Run Oni through AEI
File size: 3.8 KB
Line 
1package net.oni2.aeinstaller.backend.unpack;
2
3import java.io.File;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.util.Enumeration;
8import java.util.zip.ZipEntry;
9import java.util.zip.ZipException;
10import java.util.zip.ZipFile;
11
12import org.apache.commons.io.FileUtils;
13
14/**
15 * @author Christian Illy
16 */
17public class Unpacker implements Runnable {
18 /**
19 * @author Christian Illy
20 */
21 public enum EState {
22 /**
23 * Unpacker initialized but not started
24 */
25 INIT,
26 /**
27 * Unpack running
28 */
29 RUNNING,
30 /**
31 * Unpack interrupted
32 */
33 INTERRUPTED,
34 /**
35 * Unpack finished successfully
36 */
37 FINISHED,
38 };
39
40 private UnpackListener listener;
41
42 private File zip;
43 private File target;
44
45 private Thread t = null;
46
47 private EState state = EState.INIT;
48
49 /**
50 * Initialize a new AE package unpacker
51 *
52 * @param zipFile
53 * AE zip package
54 * @param targetFolder
55 * Target folder
56 * @param listener
57 * Listener for progress updates
58 */
59 public Unpacker(File zipFile, File targetFolder, UnpackListener listener) {
60 this.listener = listener;
61 zip = zipFile;
62 target = targetFolder;
63 }
64
65 /**
66 * Start the unpack process
67 */
68 public synchronized void start() {
69 if (t == null) {
70 t = new Thread(this);
71 t.start();
72 state = EState.RUNNING;
73 updateStatus();
74 }
75 }
76
77 /**
78 * Stop (abort) the process
79 */
80 public synchronized void stop() {
81 if (state != EState.FINISHED) {
82 state = EState.INTERRUPTED;
83 if (t != null) {
84 try {
85 t.join();
86 } catch (InterruptedException e) {
87 // TODO Auto-generated catch block
88 e.printStackTrace();
89 }
90 t = null;
91 }
92 updateStatus();
93 if (state != EState.FINISHED) {
94 if (target.exists()) {
95 try {
96 FileUtils.deleteDirectory(target);
97 } catch (IOException e) {
98 // TODO Auto-generated catch block
99 e.printStackTrace();
100 }
101 }
102 }
103 }
104 }
105
106 private synchronized void updateStatus() {
107 listener.statusUpdate(this, state);
108 }
109
110 @Override
111 public void run() {
112 try {
113 switch (state) {
114 case INTERRUPTED:
115 return;
116 case RUNNING:
117 try {
118 int pathStart = 0;
119 String pathStartName = "";
120
121 ZipFile zf = new ZipFile(zip);
122 target.mkdirs();
123 for (Enumeration<? extends ZipEntry> e = zf.entries(); e
124 .hasMoreElements();) {
125 ZipEntry ze = e.nextElement();
126 if (ze.getName().toLowerCase()
127 .endsWith("/mod_info.cfg")
128 || ze.getName().toLowerCase()
129 .equals("mod_info.cfg")) {
130 pathStart = ze.getName().toLowerCase()
131 .indexOf("mod_info.cfg");
132 pathStartName = ze.getName().substring(0,
133 pathStart);
134 }
135 }
136
137 for (Enumeration<? extends ZipEntry> e = zf.entries(); e
138 .hasMoreElements();) {
139 if (state == EState.INTERRUPTED)
140 return;
141 ZipEntry ze = e.nextElement();
142 if (!ze.isDirectory()) {
143 if (ze.getName().startsWith(pathStartName)) {
144 File targetFile = new File(target, ze
145 .getName().substring(pathStart));
146 File parent = targetFile.getParentFile();
147 parent.mkdirs();
148
149 InputStream in = zf.getInputStream(ze);
150
151 int read = 0;
152 byte[] data = new byte[1024];
153 FileOutputStream fileOut = new FileOutputStream(
154 targetFile);
155 while ((read = in.read(data, 0, 1024)) != -1) {
156 fileOut.write(data, 0, read);
157 }
158 fileOut.close();
159 }
160 }
161 }
162 } catch (ZipException e) {
163 // TODO Auto-generated catch block
164 e.printStackTrace();
165 } catch (IOException e) {
166 // TODO Auto-generated catch block
167 e.printStackTrace();
168 }
169 break;
170 default:
171 break;
172 }
173 } finally {
174 }
175
176 state = EState.FINISHED;
177 updateStatus();
178 }
179}
Note: See TracBrowser for help on using the repository browser.