Advertisement
Tyluur

Untitled

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