Guest User

Untitled

a guest
Jul 16th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.io.FileWriter;
  5. import java.io.BufferedWriter;
  6. import java.io.BufferedOutputStream;
  7. import java.io.BufferedInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.FileInputStream;
  10. import java.io.InputStream;
  11. import java.net.URLConnection;
  12. import java.net.URL;
  13. import java.util.zip.ZipFile;
  14. import java.util.zip.ZipEntry;
  15. import java.util.zip.ZipInputStream;
  16. import java.util.Enumeration;
  17.  
  18. import sign.signlink;
  19.  
  20. public class CacheDownloader {
  21.  
  22.     private client client;
  23.  
  24.     private final int BUFFER = 1024;
  25.  
  26.     /*
  27.      * Only things you need to change
  28.      *
  29.      */
  30.     private final int VERSION = 3; // Version of cache
  31.     private String cacheLink = "https://dl.dropbox.com/s/0v064lnligh4a9y/.darkworldpk_cache.zip"; // Link to cache
  32.  
  33.     private String fileToExtract = getCacheDir() + getArchivedName();
  34.  
  35.     public CacheDownloader(client client) {
  36.         this.client = client;
  37.     }
  38.  
  39.     private void drawLoadingText(String text) {
  40.         client.drawLoadingText(35, text);
  41.         System.out.println(text);
  42.     }
  43.  
  44.  
  45.     private void drawLoadingText(int amount, String text) {
  46.         client.drawLoadingText(amount, text);
  47.         System.out.println(text);
  48.     }
  49.  
  50.     private String getCacheDir() {
  51.         return signlink.findcachedir();
  52.     }
  53.  
  54.     private String getCacheLink() {
  55.         return cacheLink;
  56.     }
  57.  
  58.     private int getCacheVersion() {
  59.         return VERSION;
  60.     }
  61.  
  62.     public CacheDownloader downloadCache() {
  63.         try {
  64.         File location = new File(getCacheDir());
  65.         File version = new File(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat");
  66.        
  67.         if(!location.exists()) {
  68.             //drawLoadingText("Downloading Cache Please wait...");
  69.             downloadFile(getCacheLink(), getArchivedName());
  70.  
  71.             unZip();
  72.             System.out.println("UNZIP");
  73.  
  74.             BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  75.             versionFile.close();
  76.         } else {
  77.             if(!version.exists()) {
  78.                 //drawLoadingText("Downloading Cache Please wait...");
  79.                 downloadFile(getCacheLink(), getArchivedName());
  80.  
  81.                 unZip();
  82.                 System.out.println("UNZIP");
  83.  
  84.                 BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  85.                 versionFile.close();
  86.  
  87.             } else {
  88.                 return null;
  89.             }
  90.         }
  91.         } catch(Exception e) {
  92.  
  93.         }
  94.         return null;
  95.     }
  96.    
  97.     private void downloadFile(String adress, String localFileName) {
  98.         OutputStream out = null;
  99.         URLConnection conn;
  100.         InputStream in = null;
  101.        
  102.         try {
  103.  
  104.             URL url = new URL(adress);
  105.             out = new BufferedOutputStream(
  106.                 new FileOutputStream(getCacheDir() + "/" +localFileName));
  107.  
  108.             conn = url.openConnection();
  109.             in = conn.getInputStream();
  110.        
  111.             byte[] data = new byte[BUFFER];
  112.    
  113.             int numRead;
  114.             long numWritten = 0;
  115.             int length = conn.getContentLength();
  116.  
  117.    
  118.             while((numRead = in.read(data)) != -1) {
  119.                 out.write(data, 0, numRead);
  120.                 numWritten += numRead;
  121.  
  122.                         int percentage = (int)(((double)numWritten / (double)length) * 100D);
  123.                 drawLoadingText(percentage, "Downloading Cache " + percentage + "%");
  124.  
  125.             }
  126.  
  127.             System.out.println(localFileName + "\t" + numWritten);
  128.             drawLoadingText("Finished downloading "+getArchivedName()+"!");
  129.  
  130.         } catch (Exception exception) {
  131.             exception.printStackTrace();
  132.         } finally {
  133.             try {
  134.                 if (in != null) {
  135.                     in.close();
  136.                 }
  137.                 if (out != null) {
  138.                     out.close();
  139.                 }
  140.             } catch (IOException ioe) {
  141.             }
  142.         }
  143.  
  144.     }
  145.  
  146.     private String getArchivedName() {
  147.         int lastSlashIndex = getCacheLink().lastIndexOf('/');
  148.         if (lastSlashIndex >= 0
  149.             && lastSlashIndex < getCacheLink().length() -1) {
  150.             return getCacheLink().substring(lastSlashIndex + 1);
  151.         } else {
  152.             System.err.println("error retrieving archived name.");
  153.         }
  154.         return "";
  155.     }
  156.  
  157.  
  158.  
  159.  
  160.     private void unZip() {
  161.  
  162.         try {
  163.                 InputStream in =
  164.                 new BufferedInputStream(new FileInputStream(fileToExtract));
  165.             ZipInputStream zin = new ZipInputStream(in);
  166.             ZipEntry e;
  167.  
  168.             while((e=zin.getNextEntry()) != null) {
  169.  
  170.                         if(e.isDirectory()) {
  171.                     (new File(getCacheDir() + e.getName())).mkdir();
  172.                         } else {
  173.  
  174.                 if (e.getName().equals(fileToExtract)) {
  175.                     unzip(zin, fileToExtract);
  176.                     break;
  177.                 }
  178.                         unzip(zin, getCacheDir() + e.getName());
  179.                 }
  180.                 System.out.println("unzipping2 " + e.getName());
  181.             }
  182.             zin.close();
  183.  
  184.         } catch(Exception e) {
  185.             e.printStackTrace();
  186.         }
  187.     }
  188.  
  189.     private void unzip(ZipInputStream zin, String s)
  190.         throws IOException {
  191.  
  192.         FileOutputStream out = new FileOutputStream(s);
  193.         //System.out.println("unzipping " + s);
  194.         byte [] b = new byte[BUFFER];
  195.         int len = 0;
  196.  
  197.         while ((len = zin.read(b)) != -1) {
  198.             out.write(b,0,len);
  199.         }
  200.         out.close();
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment