[604] | 1 | package net.oni2.aeinstaller.backend.unpack;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.FileOutputStream;
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.io.InputStream;
|
---|
| 7 | import java.util.Enumeration;
|
---|
| 8 | import java.util.zip.ZipEntry;
|
---|
| 9 | import java.util.zip.ZipException;
|
---|
| 10 | import java.util.zip.ZipFile;
|
---|
| 11 |
|
---|
| 12 | import org.apache.commons.io.FileUtils;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * @author Christian Illy
|
---|
| 16 | */
|
---|
| 17 | public 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 | e.printStackTrace();
|
---|
| 88 | }
|
---|
| 89 | t = null;
|
---|
| 90 | }
|
---|
| 91 | updateStatus();
|
---|
| 92 | if (state != EState.FINISHED) {
|
---|
| 93 | if (target.exists()) {
|
---|
| 94 | try {
|
---|
| 95 | FileUtils.deleteDirectory(target);
|
---|
| 96 | } catch (IOException e) {
|
---|
| 97 | e.printStackTrace();
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | private synchronized void updateStatus() {
|
---|
| 105 | listener.statusUpdate(this, state);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | @Override
|
---|
| 109 | public void run() {
|
---|
| 110 | try {
|
---|
| 111 | switch (state) {
|
---|
| 112 | case INTERRUPTED:
|
---|
| 113 | return;
|
---|
| 114 | case RUNNING:
|
---|
[615] | 115 | ZipFile zf = null;
|
---|
[604] | 116 | try {
|
---|
| 117 | int pathStart = 0;
|
---|
[605] | 118 | String pathStartName = "";
|
---|
[604] | 119 |
|
---|
[615] | 120 | zf = new ZipFile(zip);
|
---|
[604] | 121 | target.mkdirs();
|
---|
| 122 | for (Enumeration<? extends ZipEntry> e = zf.entries(); e
|
---|
| 123 | .hasMoreElements();) {
|
---|
| 124 | ZipEntry ze = e.nextElement();
|
---|
| 125 | if (ze.getName().toLowerCase()
|
---|
[605] | 126 | .endsWith("/mod_info.cfg")
|
---|
| 127 | || ze.getName().toLowerCase()
|
---|
| 128 | .equals("mod_info.cfg")) {
|
---|
[604] | 129 | pathStart = ze.getName().toLowerCase()
|
---|
| 130 | .indexOf("mod_info.cfg");
|
---|
[605] | 131 | pathStartName = ze.getName().substring(0,
|
---|
| 132 | pathStart);
|
---|
[604] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | for (Enumeration<? extends ZipEntry> e = zf.entries(); e
|
---|
| 137 | .hasMoreElements();) {
|
---|
| 138 | if (state == EState.INTERRUPTED)
|
---|
| 139 | return;
|
---|
| 140 | ZipEntry ze = e.nextElement();
|
---|
| 141 | if (!ze.isDirectory()) {
|
---|
[605] | 142 | if (ze.getName().startsWith(pathStartName)) {
|
---|
| 143 | File targetFile = new File(target, ze
|
---|
| 144 | .getName().substring(pathStart));
|
---|
| 145 | File parent = targetFile.getParentFile();
|
---|
| 146 | parent.mkdirs();
|
---|
[604] | 147 |
|
---|
[605] | 148 | InputStream in = zf.getInputStream(ze);
|
---|
[604] | 149 |
|
---|
[605] | 150 | int read = 0;
|
---|
| 151 | byte[] data = new byte[1024];
|
---|
| 152 | FileOutputStream fileOut = new FileOutputStream(
|
---|
| 153 | targetFile);
|
---|
| 154 | while ((read = in.read(data, 0, 1024)) != -1) {
|
---|
| 155 | fileOut.write(data, 0, read);
|
---|
| 156 | }
|
---|
| 157 | fileOut.close();
|
---|
[604] | 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | } catch (ZipException e) {
|
---|
| 162 | // TODO Auto-generated catch block
|
---|
| 163 | e.printStackTrace();
|
---|
| 164 | } catch (IOException e) {
|
---|
| 165 | // TODO Auto-generated catch block
|
---|
| 166 | e.printStackTrace();
|
---|
[615] | 167 | } finally {
|
---|
| 168 | try {
|
---|
| 169 | if (zf != null)
|
---|
| 170 | zf.close();
|
---|
| 171 | } catch (IOException e) {
|
---|
| 172 | e.printStackTrace();
|
---|
| 173 | }
|
---|
[604] | 174 | }
|
---|
| 175 | break;
|
---|
| 176 | default:
|
---|
| 177 | break;
|
---|
| 178 | }
|
---|
| 179 | } finally {
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | state = EState.FINISHED;
|
---|
| 183 | updateStatus();
|
---|
| 184 | }
|
---|
| 185 | }
|
---|