- import java.io.File;
- import java.io.*;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author ArrowzFtw
- */
- public class DownloadJar {
- public static void main(String[] args) {
- try {
- deleteAllClasses();
- downloadJar();
- decompressJar();
- Reflection.startup(args);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void deleteAllClasses() {
- System.out.println("Deleting class Folders");
- File file = new File(Constants.CLASS_PATH);
- File[] files = file.listFiles();
- file.delete();
- for (File instance : files) {
- boolean a = instance.delete();
- }
- }
- public static void downloadJar() throws FileNotFoundException, IOException {
- System.out.println("Downloading JAR file.");
- byte abyte0[] = null;
- InputStream is = null;
- URL url = null;
- try {
- url = new URL(Constants.JAR_LINK);
- } catch (MalformedURLException ex) {
- ex.printStackTrace();
- }
- try {
- is = url.openStream();
- abyte0 = getBytes(is);
- } catch (IOException e) {
- System.err.printf("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
- e.printStackTrace();
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- File file = new File(Constants.JAR_PATH);
- FileOutputStream fos = new FileOutputStream(file.getPath());
- fos.write(abyte0);
- fos.close();
- }
- private static void decompressJar() {
- System.out.println("Decompressing JAR");
- try {
- java.util.jar.JarFile jar = new java.util.jar.JarFile(Constants.JAR_PATH);
- java.util.Enumeration a = jar.entries();
- while (a.hasMoreElements()) {
- java.util.jar.JarEntry jarFile = (java.util.jar.JarEntry) a.nextElement();
- java.io.File f = new java.io.File(Constants.CLASS_PATH+jarFile.getName());
- String newFolder1 = f.getPath().replaceAll("\\\\"+f.getName(), "");
- File temp = new File(newFolder1);
- temp.delete();
- temp.mkdir();
- java.io.InputStream is = jar.getInputStream(jarFile); // get the input stream
- java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
- while (is.available() > 0) { // write contents of 'is' to 'fos'
- fos.write(is.read());
- }
- fos.close();
- is.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static byte[] getBytes(InputStream is) throws IOException {
- int len;
- int size = 1024;
- byte[] buf;
- if (is instanceof ByteArrayInputStream) {
- size = is.available();
- buf = new byte[size];
- len = is.read(buf, 0, size);
- } else {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- buf = new byte[size];
- while ((len = is.read(buf, 0, size)) != -1) {
- bos.write(buf, 0, len);
- }
- buf = bos.toByteArray();
- }
- return buf;
- }
- }