Advertisement
Guest User

Untitled

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