Guest User

Untitled

a guest
Jul 14th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.74 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.     private final int VERSION = 2; // Version of cache
  27.     //private String cacheLink = "https://www.dropbox.com/s/2cu4420o5roffwh/heavenx.zip"; // Link to cache
  28.     private String cacheLink = "https://dl.dropbox.com/s/1pedxa8m0dbdx29/Cache_DWPK%20%282%29.zip"; // Link to cache
  29.    
  30.     private String fileToExtract = getCacheDir() + getArchivedName();
  31.  
  32.     public CacheDownloader(client client) {
  33.         this.client = client;
  34.     }
  35.  
  36.     private void drawLoadingText(String text) {
  37.         client.drawLoadingText(35, text);
  38.         //System.out.println(text);
  39.     }
  40.  
  41.  
  42.     private void drawLoadingText(int amount, String text) {
  43.         client.drawLoadingText(amount, text);
  44.         //System.out.println(text);
  45.     }
  46.  
  47.     private String getCacheDir() {
  48.         return signlink.findcachedir();
  49.     }
  50.  
  51.     private String getCacheLink() {
  52.         return cacheLink;
  53.     }
  54.  
  55.     private int getCacheVersion() {
  56.         return VERSION;
  57.     }
  58.  
  59.     public CacheDownloader downloadCache() {
  60.         try {
  61.         File location = new File(getCacheDir());
  62.         File version = new File(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat");
  63.        
  64.         if(!location.exists()) {
  65.             //drawLoadingText("Loading new Updates....");
  66.             downloadFile(getCacheLink(), getArchivedName());
  67.  
  68.             unZip();
  69.             //System.out.println("UNZIP");
  70.  
  71.             BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  72.             versionFile.close();
  73.             deleteZIP(getArchivedName());
  74.         } else {
  75.             if(!version.exists()) {
  76.                 //drawLoadingText("~ First Time Installation, Only Once! ~");
  77.                 downloadFile(getCacheLink(), getArchivedName());
  78.  
  79.                 unZip();
  80.                 //System.out.println("UNZIP");
  81.  
  82.                 BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  83.                 versionFile.close();
  84.                 deleteZIP(getArchivedName());
  85.  
  86.             } else {
  87.                 return null;
  88.             }
  89.         }
  90.         } catch(Exception e) {
  91.  
  92.         }
  93.         return null;
  94.     }
  95.    
  96.     private void downloadFile(String adress, String localFileName) {
  97.         OutputStream out = null;
  98.         URLConnection conn;
  99.         InputStream in = null;
  100.        
  101.         try {
  102.  
  103.             URL url = new URL(adress);
  104.             out = new BufferedOutputStream(
  105.                 new FileOutputStream(getCacheDir() + "/" +localFileName));
  106.  
  107.             conn = url.openConnection();
  108.             in = conn.getInputStream();
  109.        
  110.             byte[] data = new byte[BUFFER];
  111.    
  112.             int numRead;
  113.             long numWritten = 0;
  114.             int length = conn.getContentLength();
  115.  
  116.    
  117.             while((numRead = in.read(data)) != -1) {
  118.                 out.write(data, 0, numRead);
  119.                 numWritten += numRead;
  120.  
  121.                         int percentage = (int)(((double)numWritten / (double)length) * 100D);
  122.                 drawLoadingText(percentage, "Downloading Cache " + percentage + "%...");
  123.  
  124.             }
  125.  
  126.             System.out.println(localFileName + "\t" + numWritten);
  127.             drawLoadingText("Unpacking..");
  128.  
  129.         } catch (Exception exception) {
  130.             exception.printStackTrace();
  131.         } finally {
  132.             try {
  133.                 if (in != null) {
  134.                     in.close();
  135.                 }
  136.                 if (out != null) {
  137.                     out.close();
  138.                 }
  139.             } catch (IOException ioe) {
  140.             }
  141.         }
  142.  
  143.     }
  144.  
  145.     private String getArchivedName() {
  146.         int lastSlashIndex = getCacheLink().lastIndexOf('/');
  147.         if (lastSlashIndex >= 0
  148.             && lastSlashIndex < getCacheLink().length() -1) {
  149.             return getCacheLink().substring(lastSlashIndex + 1);
  150.         } else {
  151.             //System.err.println("error retreiving archivaed name.");
  152.         }
  153.         return "";
  154.     }
  155.  
  156.  
  157.  
  158.  
  159.     private void unZip() {
  160.  
  161.         try {
  162.                 InputStream in =
  163.                 new BufferedInputStream(new FileInputStream(fileToExtract));
  164.             ZipInputStream zin = new ZipInputStream(in);
  165.             ZipEntry e;
  166.  
  167.             while((e=zin.getNextEntry()) != null) {
  168.  
  169.                         if(e.isDirectory()) {
  170.                     (new File(getCacheDir() + e.getName())).mkdir();
  171.                         } else {
  172.  
  173.                 if (e.getName().equals(fileToExtract)) {
  174.                     unzip(zin, fileToExtract);
  175.                     break;
  176.                 }
  177.                         unzip(zin, getCacheDir() + e.getName());
  178.                 }
  179.                 //System.out.println("unzipping2 " + e.getName());
  180.             }
  181.             zin.close();
  182.  
  183.         } catch(Exception e) {
  184.             e.printStackTrace();
  185.         }
  186.     }
  187.  
  188. private void deleteZIP(String fileName){
  189.     // A File object to represent the filename
  190.     File f = new File(getCacheDir() + fileName);
  191.  
  192.     // Make sure the file or directory exists and isn't write protected
  193.     if (!f.exists())
  194.       throw new IllegalArgumentException(
  195.           "Delete: no such file or directory: " + fileName);
  196.  
  197.     if (!f.canWrite())
  198.       throw new IllegalArgumentException("Delete: write protected: "
  199.           + fileName);
  200.  
  201.     // If it is a directory, make sure it is empty
  202.     if (f.isDirectory()) {
  203.       String[] files = f.list();
  204.       if (files.length > 0)
  205.         throw new IllegalArgumentException(
  206.             "Delete: directory not empty: " + fileName);
  207.     }
  208.  
  209.     // Attempt to delete it
  210.     boolean success = f.delete();
  211.  
  212.     if (!success)
  213.       throw new IllegalArgumentException("Delete: deletion failed");
  214.  
  215.     }
  216.  
  217.     private void unzip(ZipInputStream zin, String s)
  218.         throws IOException {
  219.  
  220.         FileOutputStream out = new FileOutputStream(s);
  221.         //System.out.println("unzipping " + s);
  222.         byte [] b = new byte[BUFFER];
  223.         int len = 0;
  224.  
  225.         while ((len = zin.read(b)) != -1) {
  226.             out.write(b,0,len);
  227.         }
  228.         out.close();
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment