Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 22nd, 2012  |  syntax: None  |  size: 3.72 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. import java.io.File;
  3. import java.io.*;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. /**
  10.  *
  11.  * @author ArrowzFtw
  12.  */
  13. public class DownloadJar {
  14.  
  15.     public static void main(String[] args) {
  16.         try {
  17.             deleteAllClasses();
  18.             downloadJar();
  19.             decompressJar();
  20.             Reflection.startup(args);
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.  
  26.     private static void deleteAllClasses() {
  27.         System.out.println("Deleting class Folders");
  28.         File file = new File(Constants.CLASS_PATH);
  29.         File[] files = file.listFiles();
  30.         file.delete();
  31.         for (File instance : files) {
  32.             boolean  a = instance.delete();
  33.            
  34.         }
  35.     }
  36.  
  37.     public static void downloadJar() throws FileNotFoundException, IOException {
  38.         System.out.println("Downloading JAR file.");
  39.         byte abyte0[] = null;
  40.  
  41.         InputStream is = null;
  42.         URL url = null;
  43.         try {
  44.             url = new URL(Constants.JAR_LINK);
  45.         } catch (MalformedURLException ex) {
  46.             ex.printStackTrace();
  47.         }
  48.         try {
  49.  
  50.             is = url.openStream();
  51.             abyte0 = getBytes(is);
  52.         } catch (IOException e) {
  53.             System.err.printf("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
  54.             e.printStackTrace();
  55.         } finally {
  56.             if (is != null) {
  57.                 try {
  58.                     is.close();
  59.                 } catch (IOException ex) {
  60.                     ex.printStackTrace();
  61.                 }
  62.             }
  63.         }
  64.  
  65.         File file = new File(Constants.JAR_PATH);
  66.         FileOutputStream fos = new FileOutputStream(file.getPath());
  67.         fos.write(abyte0);
  68.         fos.close();
  69.  
  70.     }
  71.  
  72.     private static void decompressJar() {
  73.         System.out.println("Decompressing JAR");
  74.         try {
  75.             java.util.jar.JarFile jar = new java.util.jar.JarFile(Constants.JAR_PATH);
  76.             java.util.Enumeration a = jar.entries();
  77.             while (a.hasMoreElements()) {
  78.                 java.util.jar.JarEntry jarFile = (java.util.jar.JarEntry) a.nextElement();
  79.                
  80.                
  81.                 java.io.File f = new java.io.File(Constants.CLASS_PATH+jarFile.getName());
  82.                 String newFolder1 = f.getPath().replaceAll("\\\\"+f.getName(), "");
  83.                
  84.                 File temp = new File(newFolder1);
  85.                 temp.delete();
  86.                 temp.mkdir();
  87.                
  88.                
  89.                 java.io.InputStream is = jar.getInputStream(jarFile); // get the input stream
  90.                 java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
  91.                 while (is.available() > 0) {  // write contents of 'is' to 'fos'
  92.                     fos.write(is.read());
  93.                 }
  94.                 fos.close();
  95.                 is.close();
  96.  
  97.             }
  98.  
  99.         } catch (Exception e) {
  100.             e.printStackTrace();
  101.         }
  102.     }
  103.  
  104.     private static byte[] getBytes(InputStream is) throws IOException {
  105.  
  106.         int len;
  107.         int size = 1024;
  108.         byte[] buf;
  109.  
  110.         if (is instanceof ByteArrayInputStream) {
  111.             size = is.available();
  112.             buf = new byte[size];
  113.             len = is.read(buf, 0, size);
  114.         } else {
  115.             ByteArrayOutputStream bos = new ByteArrayOutputStream();
  116.             buf = new byte[size];
  117.             while ((len = is.read(buf, 0, size)) != -1) {
  118.                 bos.write(buf, 0, len);
  119.             }
  120.             buf = bos.toByteArray();
  121.         }
  122.         return buf;
  123.     }
  124. }