Guest User

Deathlypvpz v4.1 cache autodownloader

a guest
Oct 11th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 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 = 2; // Version of cache
  31. private String cacheLink = "Your LINK ";
  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. }
  42.  
  43.  
  44. private void drawLoadingText(int amount, String text) {
  45. client.drawLoadingText(amount, text);
  46. System.out.println(text);
  47. }
  48.  
  49. private String getCacheDir() {
  50. return signlink.findcachedir();
  51. }
  52.  
  53. private String getCacheLink() {
  54. return cacheLink;
  55. }
  56.  
  57. private int getCacheVersion() {
  58. return VERSION;
  59. }
  60.  
  61. public CacheDownloader downloadCache() {
  62. try {
  63. File location = new File(getCacheDir());
  64. File version = new File(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat");
  65. if(!location.exists()) {
  66. downloadFile(getCacheLink(), getArchivedName());
  67. unZip();
  68. BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  69. versionFile.close();
  70. deleteZIP(getArchivedName());
  71. } else {
  72. if(!version.exists()) {
  73. downloadFile(getCacheLink(), getArchivedName());
  74. unZip();
  75. BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  76. versionFile.close();
  77. deleteZIP(getArchivedName());
  78. } else {
  79. return null;
  80. }
  81. }
  82. } catch(Exception e) {
  83. }
  84. return null;
  85. }
  86.  
  87. private void downloadFile(String adress, String localFileName) {
  88. OutputStream out = null;
  89. URLConnection conn;
  90. InputStream in = null;
  91.  
  92. try {
  93. URL url = new URL(adress);
  94. out = new BufferedOutputStream(
  95. new FileOutputStream(getCacheDir() + "/" +localFileName));
  96.  
  97. conn = url.openConnection();
  98. in = conn.getInputStream();
  99.  
  100. byte[] data = new byte[BUFFER];
  101.  
  102. int numRead;
  103. long numWritten = 0;
  104. int length = conn.getContentLength();
  105.  
  106.  
  107. while((numRead = in.read(data)) != -1) {
  108. out.write(data, 0, numRead);
  109. numWritten += numRead;
  110. int percentage = (int)(((double)numWritten / (double)length) * 100D);
  111. drawLoadingText(percentage, "Be patient... cache is downloading.");
  112. }
  113. drawLoadingText(100, "Download finished, unzipping files...");
  114.  
  115. } catch (Exception exception) {
  116. exception.printStackTrace();
  117. } finally {
  118. try {
  119. if (in != null) {
  120. in.close();
  121. }
  122. if (out != null) {
  123. out.close();
  124. }
  125. } catch (IOException ioe) {
  126. }
  127. }
  128.  
  129. }
  130.  
  131. private String getArchivedName() {
  132. int lastSlashIndex = getCacheLink().lastIndexOf('/');
  133. if (lastSlashIndex >= 0
  134. && lastSlashIndex < getCacheLink().length() -1) {
  135. return getCacheLink().substring(lastSlashIndex + 1);
  136. } else {
  137. System.err.println("Can not find archived name.");
  138. }
  139. return null;
  140. }
  141.  
  142.  
  143.  
  144. private void unZip() {
  145. try {
  146. InputStream in =
  147. new BufferedInputStream(new FileInputStream(fileToExtract));
  148. ZipInputStream zin = new ZipInputStream(in);
  149. ZipEntry e;
  150.  
  151. while((e=zin.getNextEntry()) != null) {
  152.  
  153. if(e.isDirectory()) {
  154. (new File(getCacheDir() + e.getName())).mkdir();
  155. } else {
  156. if (e.getName().equals(fileToExtract)) {
  157. unzip(zin, fileToExtract);
  158. break;
  159. }
  160. unzip(zin, getCacheDir() + e.getName());
  161. }
  162. }
  163. zin.close();
  164.  
  165. } catch(Exception e) {
  166. e.printStackTrace();
  167. }
  168. }
  169.  
  170. private void unzip(ZipInputStream zin, String s)
  171. throws IOException {
  172. FileOutputStream out = new FileOutputStream(s);
  173. byte [] b = new byte[BUFFER];
  174. int len = 0;
  175.  
  176. while ((len = zin.read(b)) != -1) {
  177. out.write(b,0,len);
  178. }
  179. out.close();
  180. }
  181.  
  182. private void deleteZIP(String fileName) {
  183. File f = new File(getCacheDir() + fileName);
  184. if (!f.exists())
  185. throw new IllegalArgumentException(
  186. "Delete: no such file or directory: " + fileName);
  187.  
  188. if (!f.canWrite())
  189. throw new IllegalArgumentException("Delete: write protected: " + fileName);
  190. if (f.isDirectory()) {
  191. String[] files = f.list();
  192. if (files.length > 0)
  193. throw new IllegalArgumentException(
  194. "Delete: directory not empty: " + fileName);
  195. }
  196. boolean success = f.delete();
  197. if (!success)
  198. throw new IllegalArgumentException("Delete: deletion failed");
  199. }
  200. }
Add Comment
Please, Sign In to add comment