Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.simplicity.client.cache;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import com.simplicity.Configuration;
- import com.simplicity.client.Client;
- import com.simplicity.client.DebuggingRunnables;
- import com.simplicity.client.signlink;
- /**
- * Enchanced cache downloader
- * Handles cache downloading & unzipping
- * @author Gabriel Hannason
- */
- public class CacheDownloader {
- private static final String CACHE_PATH = signlink.findcachedir();
- private static final int CACHE_VERSION = 9;
- private static final String TEXT_VERSION_FILE = CACHE_PATH + "cache_version.txt";
- private static String FORUMS = "http://ADDRESS.COM/.Vulcan.zip";
- @SuppressWarnings("resource")
- public static long getCurrentVersion() {
- try {
- File versionDir = new File(TEXT_VERSION_FILE);
- if (!versionDir.exists()) {
- versionDir.createNewFile();
- return -1;
- }
- return Long.parseLong(new BufferedReader(new InputStreamReader(new FileInputStream(TEXT_VERSION_FILE))).readLine());
- } catch (Exception e) {
- return -1;
- }
- }
- public static void init() {
- try {
- for(CACHE_DOWNLOAD_FILES cacheFile : CACHE_DOWNLOAD_FILES.values()) {
- long newest = CACHE_VERSION;
- long current = getCurrentVersion();
- if (newest != current) {
- updateFiles(cacheFile);
- new FileOutputStream(TEXT_VERSION_FILE).write(String.valueOf(newest).getBytes());
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- public static void updateFiles(CACHE_DOWNLOAD_FILES cacheFile) throws Exception {
- int total = cacheFile.file.length;
- int current = 1;
- for (String file : cacheFile.file) {
- downloadFile(cacheFile, file, current, total);
- if (file.endsWith(".zip")) {
- unzip(new File(CACHE_PATH + file));
- }
- current++;
- }
- }
- public static void downloadFile(CACHE_DOWNLOAD_FILES cacheFile, String file, int current, int total) throws IOException {
- //String fileURL = cacheFile.link;
- String downloadingText = Client.optimizeText(cacheFile.toString().toLowerCase());
- URL url = new URL(FORUMS);
- HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
- httpConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
- int responseCode = httpConn.getResponseCode();
- // always check HTTP response code first
- if (responseCode == HttpURLConnection.HTTP_OK) {
- // opens input stream from the HTTP connection
- InputStream inputStream = httpConn.getInputStream();
- String saveFilePath = signlink.findcachedir() + file;
- // opens an output stream to save into file
- FileOutputStream outputStream = new FileOutputStream(saveFilePath);
- int bytesRead = -1;
- byte[] buffer = new byte[4096];
- long startTime = System.currentTimeMillis();
- int downloaded = 0;
- long numWritten = 0;
- int length = httpConn.getContentLength();
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead);
- numWritten += bytesRead;
- downloaded += bytesRead;
- int percentage = (int)(((double)numWritten / (double)length) * 100D);
- int downloadSpeed = (int) ((downloaded / 1024) / (1 + ((System.currentTimeMillis() - startTime) / 1000)));
- String s = total > 1 ? "("+current+"/"+total+")" : "";
- drawLoadingText(percentage, (new StringBuilder()).append("Installing "+downloadingText+""+s+": "+percentage+"%").toString());
- }
- outputStream.close();
- inputStream.close();
- } else {
- System.out.println("download link replied HTTP code: " + responseCode);
- }
- httpConn.disconnect();
- }
- private static void drawLoadingText(int amount, String text) {
- Client.getClient().drawLoadingText(amount, text);
- }
- private static void unzip(final File file) {
- try {
- InputStream in = new BufferedInputStream(new FileInputStream(file));
- ZipInputStream zin = new ZipInputStream(in);
- ZipEntry e;
- while((e=zin.getNextEntry()) != null) {
- if(e.isDirectory()) {
- (new File(signlink.findcachedir() + e.getName())).mkdir();
- } else {
- if (e.getName().equals(file.getName())) {
- unzipPartlyArchive(zin, file.getName());
- break;
- }
- unzipPartlyArchive(zin, signlink.findcachedir() + e.getName());
- }
- }
- zin.close();
- file.delete();
- } catch(Exception e) {}
- }
- /**
- * Unzips a partly archive
- * @param zin The zip inputstream
- * @param s The location of the zip file
- * @throws IOException The method can throw an IOException.
- */
- private static void unzipPartlyArchive(ZipInputStream zin, String s) throws Exception {
- FileOutputStream out = new FileOutputStream(s);
- drawLoadingText(100, "Loading 182 OSRS Data..");
- byte [] b = new byte[1024];
- int len = 0;
- while ((len = zin.read(b)) != -1) {
- out.write(b,0,len);
- }
- out.close();
- }
- enum CACHE_DOWNLOAD_FILES {
- CACHE(new String[]{"cache.zip"}, "CACHE_VER"),
- ;
- CACHE_DOWNLOAD_FILES(String[] file, String identifier) {
- this.file = file;
- this.identifier = identifier;
- }
- private String[] file;
- private String identifier;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement