Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. package com.client;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.FileWriter;
  7. import java.io.BufferedWriter;
  8. import java.io.BufferedOutputStream;
  9. import java.io.BufferedInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.FileInputStream;
  12. import java.io.InputStream;
  13. import java.net.URLConnection;
  14. import java.net.URL;
  15. import java.nio.file.Files;
  16. import java.nio.file.Path;
  17. import java.nio.file.Paths;
  18. import java.util.zip.ZipEntry;
  19. import java.util.zip.ZipInputStream;
  20.  
  21. import com.client.sign.Signlink;
  22.  
  23. public class CacheDownloader {
  24.  
  25. private Client client;
  26.  
  27. private final int BUFFER = 1024;
  28.  
  29. private final int VERSION = 3;
  30. private String CACHE_LINK = Configuration.CACHE_LINK;
  31.  
  32. private Path FILE_LOCATION = Paths.get(getCacheDir(), getArchivedName());
  33.  
  34. public CacheDownloader(Client client) {
  35. this.client = client;
  36. }
  37.  
  38. private void drawLoadingText(String text) {
  39. client.drawLoadingText(35, text);
  40. System.out.println(text);
  41. }
  42.  
  43. private void drawLoadingText(int amount, String text, int downloadSpeed, int timeRemaining) {
  44. client.drawLoadingText(amount, text);
  45. }
  46.  
  47. private String getCacheDir() {
  48. return Signlink.getCacheDirectory();
  49. }
  50.  
  51. private String getCacheLink() {
  52. return CACHE_LINK;
  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. if (!location.exists()) {
  64. System.out.println("Location does not exist, downloading.");
  65. downloadFile(getCacheLink(), getArchivedName());
  66.  
  67. unZip();
  68.  
  69. BufferedWriter versionFile = new BufferedWriter(new FileWriter(
  70. getCacheDir() + "/cacheVersion" + getCacheVersion()
  71. + ".dat"));
  72. versionFile.close();
  73. } else {
  74. if (!version.exists()) {
  75. downloadFile(getCacheLink(), getArchivedName());
  76.  
  77. unZip();
  78.  
  79. BufferedWriter versionFile = new BufferedWriter(
  80. new FileWriter(getCacheDir() + "/cacheVersion"
  81. + getCacheVersion() + ".dat"));
  82. versionFile.close();
  83. } else {
  84. return null;
  85. }
  86. }
  87. } catch (Exception e) {
  88. e.printStackTrace();
  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. URL url = new URL(adress);
  100. out = new BufferedOutputStream(new FileOutputStream(getCacheDir() + "/" + localFileName));
  101.  
  102. conn = url.openConnection();
  103. in = conn.getInputStream();
  104.  
  105. byte[] data = new byte[BUFFER];
  106.  
  107. int numRead;
  108. long numWritten = 0;
  109. int fileSize = conn.getContentLength();
  110. long startTime = System.currentTimeMillis();
  111.  
  112. while ((numRead = in.read(data)) != -1) {
  113. out.write(data, 0, numRead);
  114. numWritten += numRead;
  115.  
  116. int percentage = (int) (((double) numWritten / (double) fileSize) * 100D);
  117. long elapsedTime = System.currentTimeMillis() - startTime;
  118. int downloadSpeed = (int) ((numWritten / 1024) / (1 + (elapsedTime / 1000)));
  119.  
  120. float speedInBytes = 1000f * numWritten / elapsedTime;
  121. int timeRemaining = (int) ((fileSize - numWritten) / speedInBytes);
  122.  
  123. drawLoadingText(percentage, "Downloading Generic Cache" + percentage + "% (only happens once)", downloadSpeed, timeRemaining);
  124. }
  125. System.out.println(localFileName + "\t" + numWritten);
  126. drawLoadingText("Generic Files - Unzipping...");
  127. } catch (Exception exception) {
  128. exception.printStackTrace();
  129. } finally {
  130. try {
  131. if (in != null) {
  132. in.close();
  133. }
  134. if (out != null) {
  135. out.close();
  136. }
  137. } catch (IOException ioe) {
  138. ioe.printStackTrace();
  139. }
  140. }
  141. }
  142.  
  143. private String getArchivedName() {
  144. int lastSlashIndex = getCacheLink().lastIndexOf('/');
  145. if (lastSlashIndex >= 0 && lastSlashIndex < getCacheLink().length() - 1) {
  146. String u = getCacheLink().substring(lastSlashIndex + 1);
  147. String Name = u.replace("?dl=1", "");
  148. return Name;
  149. } else {
  150. System.err.println("error retrieving archived name.");
  151. }
  152. return "";
  153. }
  154.  
  155. private void unZip() throws IOException {
  156. InputStream in = new BufferedInputStream(new FileInputStream(FILE_LOCATION.toString()));
  157. ZipInputStream zin = new ZipInputStream(in);
  158. ZipEntry e;
  159. while ((e = zin.getNextEntry()) != null) {
  160. String fileName = e.getName();
  161. File newFile = new File(getCacheDir() + File.separator + fileName);
  162. if (e.isDirectory()) {
  163. (new File(getCacheDir() + e.getName())).mkdir();
  164. } else {
  165.  
  166. if (e.getName().equals(FILE_LOCATION)) {
  167. unzip(zin, FILE_LOCATION.toString());
  168. break;
  169. }
  170. new File(newFile.getParent()).mkdirs();
  171. unzip(zin, getCacheDir() + e.getName());
  172. }
  173. System.out.println("unzipping2 " + e.getName());
  174. }
  175. zin.close();
  176. Files.deleteIfExists(FILE_LOCATION);
  177. }
  178.  
  179. private void unzip(ZipInputStream zin, String s) throws IOException {
  180. FileOutputStream out = new FileOutputStream(s);
  181. byte[] b = new byte[BUFFER];
  182. int len = 0;
  183. while ((len = zin.read(b)) != -1)
  184. out.write(b, 0, len);
  185.  
  186. out.close();
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement