Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1. package com.simplicity.client.cache;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.net.HttpURLConnection;
  15. import java.net.URL;
  16. import java.util.zip.ZipEntry;
  17. import java.util.zip.ZipInputStream;
  18.  
  19. import com.simplicity.Configuration;
  20. import com.simplicity.client.Client;
  21. import com.simplicity.client.DebuggingRunnables;
  22. import com.simplicity.client.signlink;
  23.  
  24.  
  25. /**
  26.  * Enchanced cache downloader
  27.  * Handles cache downloading & unzipping
  28.  * @author Gabriel Hannason
  29.  */
  30. public class CacheDownloader {
  31.    
  32.     private static final String CACHE_PATH = signlink.findcachedir();
  33.    
  34.     private static final int CACHE_VERSION = 9;
  35.    
  36.     private static final String TEXT_VERSION_FILE = CACHE_PATH + "cache_version.txt";
  37.    
  38.     private static String FORUMS = "http://ADDRESS.COM/.Vulcan.zip";
  39.    
  40.     @SuppressWarnings("resource")
  41.     public static long getCurrentVersion() {
  42.         try {
  43.             File versionDir = new File(TEXT_VERSION_FILE);
  44.  
  45.             if (!versionDir.exists()) {
  46.                 versionDir.createNewFile();
  47.                 return -1;
  48.             }
  49.  
  50.             return Long.parseLong(new BufferedReader(new InputStreamReader(new FileInputStream(TEXT_VERSION_FILE))).readLine());
  51.         } catch (Exception e) {
  52.             return -1;
  53.         }
  54.     }
  55.     public static void init() {
  56.         try {
  57.             for(CACHE_DOWNLOAD_FILES cacheFile : CACHE_DOWNLOAD_FILES.values()) {
  58.  
  59.                     long newest = CACHE_VERSION;
  60.                     long current = getCurrentVersion();
  61.                    
  62.                     if (newest != current) {
  63.                         updateFiles(cacheFile);
  64.                         new FileOutputStream(TEXT_VERSION_FILE).write(String.valueOf(newest).getBytes());
  65.                 }
  66.             }
  67.            
  68.         } catch(Exception e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.    
  73.     public static void updateFiles(CACHE_DOWNLOAD_FILES cacheFile) throws Exception {
  74.         int total = cacheFile.file.length;
  75.         int current = 1;
  76.        
  77.         for (String file : cacheFile.file) {
  78.             downloadFile(cacheFile, file, current, total);
  79.             if (file.endsWith(".zip")) {
  80.                 unzip(new File(CACHE_PATH + file));
  81.             }
  82.             current++;
  83.         }
  84.        
  85.  
  86.     }
  87.    
  88.     public static void downloadFile(CACHE_DOWNLOAD_FILES cacheFile, String file, int current, int total) throws IOException {
  89.         //String fileURL = cacheFile.link;
  90.         String downloadingText = Client.optimizeText(cacheFile.toString().toLowerCase());
  91.         URL url = new URL(FORUMS);
  92.         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
  93.         httpConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
  94.         int responseCode = httpConn.getResponseCode();
  95.  
  96.         // always check HTTP response code first
  97.         if (responseCode == HttpURLConnection.HTTP_OK) {
  98.             // opens input stream from the HTTP connection
  99.             InputStream inputStream = httpConn.getInputStream();
  100.             String saveFilePath = signlink.findcachedir() + file;
  101.             // opens an output stream to save into file
  102.             FileOutputStream outputStream = new FileOutputStream(saveFilePath);
  103.  
  104.             int bytesRead = -1;
  105.             byte[] buffer = new byte[4096];
  106.             long startTime = System.currentTimeMillis();
  107.             int downloaded = 0;
  108.             long numWritten = 0;
  109.             int length = httpConn.getContentLength();
  110.             while ((bytesRead = inputStream.read(buffer)) != -1) {
  111.                 outputStream.write(buffer, 0, bytesRead);
  112.                 numWritten += bytesRead;
  113.                 downloaded += bytesRead;
  114.                 int percentage = (int)(((double)numWritten / (double)length) * 100D);
  115.                 int downloadSpeed = (int) ((downloaded / 1024) / (1 + ((System.currentTimeMillis() - startTime) / 1000)));
  116.                 String s = total > 1 ? "("+current+"/"+total+")" : "";
  117.                 drawLoadingText(percentage, (new StringBuilder()).append("Installing "+downloadingText+""+s+": "+percentage+"%").toString());
  118.             }
  119.            
  120.        
  121.             outputStream.close();
  122.             inputStream.close();
  123.  
  124.         } else {
  125.             System.out.println("download link replied HTTP code: " + responseCode);
  126.         }
  127.         httpConn.disconnect();
  128.     }
  129.  
  130.     private static void drawLoadingText(int amount, String text) {
  131.         Client.getClient().drawLoadingText(amount, text);
  132.     }
  133.  
  134.     private static void unzip(final File file) {
  135.  
  136.         try {
  137.             InputStream in =  new BufferedInputStream(new FileInputStream(file));
  138.             ZipInputStream zin = new ZipInputStream(in);
  139.             ZipEntry e;
  140.             while((e=zin.getNextEntry()) != null) {
  141.                 if(e.isDirectory()) {
  142.                     (new File(signlink.findcachedir() + e.getName())).mkdir();
  143.                 } else {
  144.                     if (e.getName().equals(file.getName())) {
  145.                         unzipPartlyArchive(zin, file.getName());
  146.                         break;
  147.                     }
  148.                     unzipPartlyArchive(zin, signlink.findcachedir() + e.getName());
  149.                 }
  150.             }
  151.             zin.close();
  152.             file.delete();
  153.        
  154.         } catch(Exception e) {}
  155.     }
  156.  
  157.     /**
  158.      * Unzips a partly archive
  159.      * @param zin   The zip inputstream
  160.      * @param s     The location of the zip file
  161.      * @throws IOException  The method can throw an IOException.
  162.      */
  163.     private static void unzipPartlyArchive(ZipInputStream zin, String s) throws Exception {
  164.         FileOutputStream out = new FileOutputStream(s);
  165.         drawLoadingText(100, "Loading 182 OSRS Data..");
  166.         byte [] b = new byte[1024];
  167.         int len = 0;
  168.  
  169.         while ((len = zin.read(b)) != -1) {
  170.             out.write(b,0,len);
  171.         }
  172.         out.close();
  173.     }
  174.  
  175.    
  176.     enum CACHE_DOWNLOAD_FILES {
  177.         CACHE(new String[]{"cache.zip"}, "CACHE_VER"),
  178.         ;
  179.         CACHE_DOWNLOAD_FILES(String[] file, String identifier) {
  180.             this.file = file;
  181.             this.identifier = identifier;
  182.         }
  183.         private String[] file;
  184.         private String identifier;
  185.    
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement