Guest User

Cachedownloader.java

a guest
Apr 7th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.zip.*;
  4. import java.util.Enumeration;
  5. import sign.signlink;
  6.  
  7. public class CacheDownloader {
  8.  
  9. public client client;
  10. public final int BUFFER = 1024;
  11. public static final int VERSION = 1;
  12. public String cacheLink = "http://dl.dropbox.com/u/54429218/CridCacheV1.zip";
  13. public String fileToExtract = getCacheDir() + getArchivedName();
  14.  
  15. public CacheDownloader(client client) {
  16. this.client = client;
  17. }
  18.  
  19. public void drawLoadingText(String text) {
  20. client.drawLoadingText(35, text);
  21. }
  22.  
  23. public void drawLoadingText(int amount, String text) {
  24. client.drawLoadingText(amount, text);
  25. }
  26.  
  27. public String getCacheDir() {
  28. return signlink.findcachedir();
  29. }
  30.  
  31. public String getCacheLink() {
  32. return cacheLink;
  33. }
  34.  
  35. public int getCacheVersion() {
  36. return VERSION;
  37. }
  38.  
  39. public CacheDownloader downloadCache() {
  40. try {
  41. File location = new File(getCacheDir());
  42. File version = new File(getCacheDir() + "/cacheUpdate" + getCacheVersion() + ".dat");
  43. if(!location.exists()) {
  44. //delete(getCacheDir());
  45. downloadFile(getCacheLink(), getArchivedName());
  46. unZip();
  47. BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheUpdate" + getCacheVersion() + ".dat"));
  48. versionFile.close();
  49. deleteDownloadedZipFile(getArchivedName());
  50. } else {
  51. if(!version.exists()) {
  52. //delete(getCacheDir());
  53. downloadFile(getCacheLink(), getArchivedName());
  54. unZip();
  55. BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheUpdate" + getCacheVersion() + ".dat"));
  56. versionFile.close();
  57. deleteDownloadedZipFile(getArchivedName());
  58. } else {
  59. return null;
  60. }
  61. }
  62. } catch(Exception e) {
  63. }
  64. return null;
  65. }
  66.  
  67. public void downloadFile(String adress, String localFileName) {
  68. OutputStream out = null;
  69. URLConnection conn;
  70. InputStream in = null;
  71. try {
  72. URL url = new URL(adress);
  73. out = new BufferedOutputStream(new FileOutputStream(getCacheDir() + "/" +localFileName));
  74. conn = url.openConnection();
  75. in = conn.getInputStream();
  76. byte[] data = new byte[BUFFER];
  77. int numRead;
  78. long numWritten = 0;
  79. int length = conn.getContentLength();
  80. while((numRead = in.read(data)) != -1) {
  81. out.write(data, 0, numRead);
  82. numWritten += numRead;
  83. int percentage = (int)(((double)numWritten / (double)length) * 100D);
  84. drawLoadingText(percentage, "Downloading Cache " + percentage + "%");
  85. }
  86. drawLoadingText("Finished downloading "+getArchivedName()+"!");
  87. } catch (Exception exception) {
  88. exception.printStackTrace();
  89. } finally {
  90. try {
  91. if (in != null) {
  92. in.close();
  93. }
  94. if (out != null) {
  95. out.close();
  96. }
  97. } catch (IOException ioe) {
  98. }
  99. }
  100. }
  101.  
  102. public String getArchivedName() {
  103. int lastSlashIndex = getCacheLink().lastIndexOf('/');
  104. if (lastSlashIndex >= 0
  105. && lastSlashIndex < getCacheLink().length() -1) {
  106. return getCacheLink().substring(lastSlashIndex + 1);
  107. } else {
  108. }
  109. return "";
  110. }
  111.  
  112. public void deleteDownloadedZipFile(String fileName) {
  113. File f = new File(getCacheDir() + fileName);
  114. if (!f.exists())
  115. throw new IllegalArgumentException("Delete: no such file or directory: " + fileName);
  116. if (!f.canWrite())
  117. throw new IllegalArgumentException("Delete: write protected: " + fileName);
  118. if (f.isDirectory()) {
  119. String[] files = f.list();
  120. if (files.length > 0)
  121. throw new IllegalArgumentException("Delete: directory not empty: " + fileName);
  122. }
  123. boolean success = f.delete();
  124. if (!success)
  125. throw new IllegalArgumentException("Delete: deletion failed");
  126. }
  127.  
  128. public static boolean delete( String path ) {
  129. if (path == null || path.trim().length() == 0)
  130. return false;
  131. return delete(new File(path));
  132. }
  133.  
  134. public static boolean delete( File fileOrDirectory ) {
  135. if (fileOrDirectory == null) return false;
  136. if (!fileOrDirectory.exists()) return false;
  137. if (fileOrDirectory.isDirectory()) {
  138. for (File childFile : fileOrDirectory.listFiles()) {
  139. delete(childFile);
  140. }
  141. }
  142. return fileOrDirectory.delete();
  143. }
  144.  
  145. public void unZip() {
  146. try {
  147. InputStream in = new BufferedInputStream(new FileInputStream(fileToExtract));
  148. ZipInputStream zin = new ZipInputStream(in);
  149. ZipEntry e;
  150. while((e=zin.getNextEntry()) != null) {
  151. if(e.isDirectory()) {
  152. (new File(getCacheDir() + e.getName())).mkdir();
  153. } else {
  154. if (e.getName().equals(fileToExtract)) {
  155. unzip(zin, fileToExtract);
  156. break;
  157. }
  158. unzip(zin, getCacheDir() + e.getName());
  159. }
  160. }
  161. zin.close();
  162. } catch(Exception e) {
  163. e.printStackTrace();
  164. }
  165. }
  166.  
  167. public void unzip(ZipInputStream zin, String s) throws IOException {
  168. FileOutputStream out = new FileOutputStream(s);
  169. byte [] b = new byte[BUFFER];
  170. int len = 0;
  171. while ((len = zin.read(b)) != -1) {
  172. out.write(b,0,len);
  173. }
  174. out.close();
  175. }
  176. }
Add Comment
Please, Sign In to add comment