/* */ package NET.worlds.core; /* */ /* */ import NET.worlds.console.Console; /* */ import NET.worlds.console.PolledDialog; /* */ import NET.worlds.network.ProgressBar; /* */ import NET.worlds.network.URL; /* */ import NET.worlds.scape.FileList; /* */ import NET.worlds.scape.Pilot; /* */ import NET.worlds.scape.World; /* */ import java.awt.Button; /* */ import java.awt.Event; /* */ import java.awt.GridBagConstraints; /* */ import java.awt.GridBagLayout; /* */ import java.awt.Insets; /* */ import java.awt.Label; /* */ import java.io.File; /* */ import java.io.FileInputStream; /* */ import java.io.FileOutputStream; /* */ import java.io.IOException; /* */ import java.text.MessageFormat; /* */ import java.util.Enumeration; /* */ import java.util.Vector; /* */ import java.util.zip.CRC32; /* */ import java.util.zip.ZipEntry; /* */ import java.util.zip.ZipFile; /* */ import java.util.zip.ZipOutputStream; /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public class ArchiveMaker /* */ extends PolledDialog /* */ { /* */ private static final long serialVersionUID = -7620030251122522576L; /* */ private static final String extensions = "cmp;mov"; /* */ private URL sourceURL; /* */ private String path; /* 50 */ private Label status = new Label("", 0); /* */ private Button goButton; /* 52 */ private Button closeButton = new Button( /* 53 */ Console.message("Close")); /* 54 */ private ProgressBar progress = new ProgressBar(1); /* */ /* */ private boolean start; /* */ private boolean stop; /* */ private boolean finished; /* */ private boolean condense; /* */ /* */ public ArchiveMaker(boolean condense) /* */ { /* 63 */ super(Console.getFrame(), null, condense ? Console.message("Condense-Files") : Console.message("Expand-Files"), true); /* 64 */ this.condense = condense; /* 65 */ ready(); /* */ } /* */ /* */ protected void build() { /* 69 */ String errStatus = null; /* 70 */ Pilot pilot = Pilot.getActive(); /* 71 */ if (pilot != null) { /* 72 */ World world = pilot.getWorld(); /* 73 */ if (world != null) { /* 74 */ this.sourceURL = world.getSourceURL(); /* 75 */ this.path = this.sourceURL.unalias(); /* 76 */ this.path = this.path.substring(0, this.path.lastIndexOf('/')); /* 77 */ String home = URL.getHome().unalias(); /* 78 */ if (this.path.startsWith(home)) { /* 79 */ File f = new File(URL.make(this.sourceURL, "content.zip") /* 80 */ .unalias()); /* 81 */ if (this.condense) { /* 82 */ if (f.exists()) { /* 83 */ errStatus = Console.message("Must-expand"); /* */ } /* 85 */ } else if (!f.exists()) { /* 86 */ errStatus = Console.message("Must-condense"); /* */ } /* */ } else { /* 89 */ errStatus = Console.message("Not-a-home"); /* */ } /* 91 */ } else { errStatus = Console.message("Cant-access-world"); /* */ } /* 93 */ } else { errStatus = Console.message("Cant-access-pilot"); /* */ } /* 95 */ GridBagLayout gbag = new GridBagLayout(); /* 96 */ setLayout(gbag); /* 97 */ GridBagConstraints c = new GridBagConstraints(); /* 98 */ c.weightx = 0.0D; /* 99 */ c.gridwidth = 1; /* 100 */ add(gbag, new Label(Console.message("Status"), 2), c); /* 101 */ c.weightx = 1.0D; /* 102 */ c.gridwidth = 0; /* 103 */ c.fill = 2; /* 104 */ add(gbag, this.status, c); /* 105 */ c.fill = 0; /* 106 */ c.weightx = 0.0D; /* 107 */ c.gridwidth = 0; /* 108 */ this.goButton = new Button(this.condense ? Console.message("Condense") : /* 109 */ Console.message("Expand")); /* 110 */ add(gbag, this.goButton, c); /* 111 */ c.weightx = 1.0D; /* 112 */ c.fill = 2; /* 113 */ Insets tmp = c.insets; /* 114 */ c.insets = new Insets(5, 5, 5, 5); /* 115 */ add(gbag, this.progress, c); /* 116 */ c.insets = tmp; /* 117 */ c.fill = 0; /* 118 */ c.weightx = 0.0D; /* 119 */ add(gbag, this.closeButton, c); /* */ /* 121 */ if (errStatus != null) { /* 122 */ this.status.setText(errStatus); /* 123 */ this.goButton.setEnabled(false); /* */ } else { /* 125 */ String name = this.sourceURL.getAbsolute(); /* 126 */ name = name.substring(0, name.lastIndexOf('/')); /* */ /* 128 */ Object[] arguments = { new String(name) }; /* 129 */ if (this.condense) { /* 130 */ this.status.setText(MessageFormat.format( /* 131 */ Console.message("condense-name"), arguments)); /* */ } else { /* 133 */ this.status.setText(MessageFormat.format( /* 134 */ Console.message("expand-name"), arguments)); /* */ } /* */ } /* */ } /* */ /* */ public boolean action(Event event, Object what) { /* 140 */ Object target = event.target; /* 141 */ if (target == this.goButton) { /* 142 */ if (!this.start) { /* 143 */ this.start = true; /* */ } else /* 145 */ this.stop = true; /* 146 */ } else if (target == this.closeButton) { /* 147 */ if ((!this.start) || (this.finished)) /* 148 */ done(true); /* */ } else /* 150 */ return false; /* 151 */ return true; /* */ } /* */ /* */ protected void activeCallback() /* */ { /* 156 */ if ((!this.start) || (this.finished)) /* 157 */ return; /* 158 */ this.goButton.setLabel(Console.message("Stop")); /* 159 */ if (this.condense) { /* 160 */ doCondense(); /* */ } else /* 162 */ doExpand(); /* */ } /* */ /* */ private void doCondense() { /* 166 */ this.status.setText(Console.message("Scanning-dir")); /* */ /* */ /* 169 */ Vector dirs = new Vector(); /* 170 */ getDirectories(dirs, this.path); /* */ /* 172 */ if (this.stop) { /* 173 */ cancelled(); /* 174 */ return; /* */ } /* */ /* */ /* 178 */ String d = ""; /* 179 */ int count = dirs.size(); /* 180 */ for (int i = 0; i < count; i++) { /* 181 */ if (d.length() != 0) /* 182 */ d = d + ";"; /* 183 */ d = d + (String)dirs.elementAt(i); /* */ } /* */ /* */ /* 187 */ Vector files = new FileList(d, "cmp;mov").keepPathInfo().dontSort() /* 188 */ .getList(); /* 189 */ count = files.size(); /* 190 */ String[] list = new String[count]; /* 191 */ int start = this.path.length() + 1; /* 192 */ for (int i = 0; i < count; i++) { /* 193 */ list[i] = ((String)files.elementAt(i)).substring(start) /* 194 */ .replace('\\', '/').toLowerCase(); /* */ } /* */ /* 197 */ this.status.setText(Console.message("Sorting-files")); /* 198 */ Sort.sort(list); /* */ /* 200 */ if (this.stop) { /* 201 */ cancelled(); /* 202 */ return; /* */ } /* */ /* */ /* 206 */ this.status.setText(Console.message("Building-archive")); /* 207 */ String zipFileName = this.path + "/" + "content.zip"; /* 208 */ boolean deleteOnError = true; /* */ try { /* 210 */ FileOutputStream fos = new FileOutputStream(zipFileName); /* 211 */ ZipOutputStream zs = new ZipOutputStream(fos); /* 212 */ zs.setMethod(0); /* 213 */ double dlen = list.length; /* 214 */ for (int i = 0; (i < list.length) && (!this.stop); i++) { /* 215 */ Object[] arguments = { new String(list[i]) }; /* 216 */ this.status.setText(MessageFormat.format( /* 217 */ Console.message("Reading-list"), arguments)); /* 218 */ File f = new File(this.path + "/" + list[i]); /* 219 */ int length = (int)f.length(); /* 220 */ byte[] data = new byte[length]; /* 221 */ FileInputStream fis = new FileInputStream(f); /* 222 */ fis.read(data); /* 223 */ fis.close(); /* 224 */ CRC32 crc = new CRC32(); /* 225 */ crc.update(data); /* 226 */ this.status.setText(MessageFormat.format( /* 227 */ Console.message("Writing-list"), arguments)); /* 228 */ ZipEntry ze = new ZipEntry(list[i]); /* 229 */ ze.setSize(length); /* 230 */ ze.setTime(f.lastModified()); /* 231 */ ze.setCrc(crc.getValue()); /* 232 */ zs.putNextEntry(ze); /* 233 */ zs.write(data, 0, length); /* 234 */ zs.closeEntry(); /* 235 */ this.progress.setProgress((i + 1) / dlen); /* */ } /* 237 */ zs.finish(); /* 238 */ zs.close(); /* 239 */ if (!this.stop) /* */ { /* */ /* 242 */ deleteOnError = false; /* 243 */ this.goButton.setEnabled(false); /* 244 */ Archive.flushAll(); /* 245 */ for (int i = 0; i < list.length; i++) { /* 246 */ Object[] arguments = { new String(list[i]) }; /* 247 */ this.status.setText(MessageFormat.format( /* 248 */ Console.message("Deleting-list"), arguments)); /* 249 */ File f = new File(this.path + "/" + list[i]); /* 250 */ f.delete(); /* 251 */ this.progress.setProgress((i + 1) / dlen); /* */ } /* */ /* */ /* 255 */ this.status.setText(Console.message("Removing-empty")); /* 256 */ count = dirs.size(); /* 257 */ for (int i = 0; i < count; i++) { /* 258 */ File f = new File((String)dirs.elementAt(i)); /* 259 */ if (f.list().length == 0) { /* 260 */ f.delete(); /* */ } /* */ } /* 263 */ this.status.setText(Console.message("Done")); /* 264 */ this.finished = true; /* 265 */ return; /* */ } /* */ } catch (IOException ioe) { /* 268 */ Object[] arguments = { new String(this.status.getText()) }; /* 269 */ this.status.setText(MessageFormat.format(Console.message("IO-Error"), /* 270 */ arguments)); /* */ /* 272 */ if (deleteOnError) { /* 273 */ File f = new File(zipFileName); /* 274 */ f.delete(); /* */ } /* 276 */ this.finished = true; /* 277 */ if (this.stop) /* 278 */ cancelled(); /* */ } /* */ } /* */ /* 282 */ private void doExpand() { this.status.setText(Console.message("Scanning-archive")); /* 283 */ boolean deleteOnError = true; /* 284 */ Vector written = new Vector(); /* */ try { /* 286 */ String zipFileName = this.path + "/" + "content.zip"; /* 287 */ ZipFile zip = new ZipFile(zipFileName); /* 288 */ Enumeration ents = zip.entries(); /* 289 */ int count = 0; /* 290 */ while ((ents.hasMoreElements()) && (!this.stop)) { /* 291 */ count++; /* 292 */ ents.nextElement(); /* */ } /* 294 */ String lastDir = ""; /* 295 */ ents = zip.entries(); /* 296 */ int i = 0; /* 297 */ double dlen = count; /* 298 */ while ((ents.hasMoreElements()) && (!this.stop)) { /* 299 */ ZipEntry ent = (ZipEntry)ents.nextElement(); /* 300 */ String name = ent.getName(); /* 301 */ Object[] arguments = { new String(name) }; /* 302 */ this.status.setText(MessageFormat.format( /* 303 */ Console.message("Checking-name"), arguments)); /* 304 */ String filePath = this.path + "/" + name; /* 305 */ File f = new File(filePath); /* 306 */ if (f.exists()) { /* 307 */ this.stop = true; /* 308 */ this.finished = true; /* 309 */ this.status.setText(MessageFormat.format( /* 310 */ Console.message("File-exists"), arguments)); /* 311 */ break; /* */ } /* 313 */ int index = filePath.lastIndexOf('/'); /* 314 */ if (index != -1) { /* 315 */ String dir = filePath.substring(0, index); /* 316 */ if (!dir.equals(lastDir)) { /* 317 */ this.status.setText(Console.message("Creating-dir")); /* 318 */ File dirs = new File(dir); /* 319 */ dirs.mkdirs(); /* 320 */ lastDir = dir; /* */ } /* */ } /* 323 */ this.status.setText(MessageFormat.format( /* 324 */ Console.message("Reading-list"), arguments)); /* 325 */ byte[] data = Archive.load(zip, ent); /* 326 */ this.status.setText(MessageFormat.format( /* 327 */ Console.message("Writing-list"), arguments)); /* 328 */ FileOutputStream fos = new FileOutputStream(f); /* 329 */ written.addElement(f); /* 330 */ fos.write(data); /* 331 */ fos.close(); /* */ /* 333 */ this.progress.setProgress(++i / dlen); /* */ } /* 335 */ zip.close(); /* 336 */ if (!this.stop) /* */ { /* 338 */ this.goButton.setEnabled(false); /* 339 */ deleteOnError = false; /* 340 */ synchronized (Archive.getMutex()) { /* 341 */ Archive.flushAll(); /* 342 */ new File(zipFileName).delete(); /* */ } /* 344 */ this.status.setText(Console.message("Done")); /* 345 */ this.finished = true; /* 346 */ return; /* */ } /* */ } catch (IOException ioe) { /* 349 */ Object[] arguments = { new String(this.status.getText()) }; /* 350 */ this.status.setText(MessageFormat.format(Console.message("IO-Error"), /* 351 */ arguments)); /* */ /* 353 */ if (deleteOnError) { /* 354 */ Enumeration e = written.elements(); /* 355 */ while (e.hasMoreElements()) /* 356 */ ((File)e.nextElement()).delete(); /* */ } /* 358 */ if (!this.finished) { /* 359 */ this.finished = true; /* 360 */ if (this.stop) /* 361 */ cancelled(); /* */ } /* */ } /* */ } /* */ /* 366 */ private void cancelled() { this.finished = true; /* 367 */ this.status.setText(Console.message("Cancelled")); /* */ } /* */ /* */ protected boolean done(boolean confirmed) /* */ { /* 372 */ if ((!this.start) || (this.finished)) /* 373 */ return super.done(confirmed); /* 374 */ return true; /* */ } /* */ /* */ private static void getDirectories(Vector dirs, String path) { /* 378 */ dirs.addElement(path); /* 379 */ File dir = new File(path); /* 380 */ String[] files = dir.list(); /* 381 */ for (int i = 0; i < files.length; i++) { /* 382 */ String name = path + '/' + files[i]; /* 383 */ File f = new File(name); /* 384 */ if (f.isDirectory()) { /* 385 */ getDirectories(dirs, name); /* */ } /* */ } /* */ } /* */ } /* Location: C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\core\ArchiveMaker.class * Java compiler version: 6 (50.0) * JD-Core Version: 0.7.1 */