Advertisement
Guest User

Untitled

a guest
Mar 30th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. CacheDownloader: Errors 1 and 4, on lines 7 and 162 the error or what I changed can be found.
  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.NovaScape;
  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.util.zip.ZipFile;
  16. import java.util.zip.ZipEntry;
  17. import java.util.zip.ZipInputStream;
  18. import java.util.Enumeration;
  19.  
  20. import sign.signlink;
  21.  
  22. public class CacheDownloader {
  23.  
  24. private client client;
  25.  
  26. private final int BUFFER = 1024;
  27.  
  28. private final int VERSION = 15; // Version of cache
  29. private String cacheLink = "https://dl.dropboxusercontent.com/u/110629812/NovaScapeCacheO.zip"; // Link to cache
  30.  
  31. private String fileToExtract = getCacheDir() + getArchivedName();
  32.  
  33. public CacheDownloader(client client) {
  34. this.client = client;
  35. }
  36.  
  37. private void drawLoadingText(String text) {
  38. client.drawLoadingText(35, text);
  39. //System.out.println(text);
  40. }
  41.  
  42.  
  43. private void drawLoadingText(int amount, String text) {
  44. client.drawLoadingText(amount, text);
  45. //System.out.println(text);
  46. }
  47.  
  48. private String getCacheDir() {
  49. return signlink.findcachedir();
  50. }
  51.  
  52. private String getCacheLink() {
  53. return cacheLink;
  54. }
  55.  
  56. private int getCacheVersion() {
  57. return VERSION;
  58. }
  59.  
  60. public CacheDownloader downloadCache() {
  61. try {
  62. File location = new File(getCacheDir());
  63. File version = new File(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat");
  64.  
  65. if(!location.exists()) {
  66. //drawLoadingText("Loading new Updates....");
  67. downloadFile(getCacheLink(), getArchivedName());
  68.  
  69. unZip();
  70. //System.out.println("UNZIP");
  71.  
  72. BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  73. versionFile.close();
  74. deleteZIP(getArchivedName());
  75. } else {
  76. if(!version.exists()) {
  77. //drawLoadingText("~ First Time Installation, Only Once! ~");
  78. downloadFile(getCacheLink(), getArchivedName());
  79.  
  80. unZip();
  81. //System.out.println("UNZIP");
  82.  
  83. BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  84. versionFile.close();
  85. deleteZIP(getArchivedName());
  86.  
  87. } else {
  88. return null;
  89. }
  90. }
  91. } catch(Exception e) {
  92.  
  93. }
  94. return null;
  95. }
  96.  
  97. private void downloadFile(String adress, String localFileName) {
  98. OutputStream out = null;
  99. URLConnection conn;
  100. InputStream in = null;
  101.  
  102. try {
  103.  
  104. URL url = new URL(adress);
  105. out = new BufferedOutputStream(
  106. new FileOutputStream(getCacheDir() + "/" +localFileName));
  107.  
  108. conn = url.openConnection();
  109. in = conn.getInputStream();
  110.  
  111. byte[] data = new byte[BUFFER];
  112.  
  113. int numRead;
  114. long numWritten = 0;
  115. int length = conn.getContentLength();
  116.  
  117.  
  118. while((numRead = in.read(data)) != -1) {
  119. out.write(data, 0, numRead);
  120. numWritten += numRead;
  121.  
  122. int percentage = (int)(((double)numWritten / (double)length) * 100D);
  123. drawLoadingText(percentage, "Downloading Cache " + percentage + "%...");
  124.  
  125. }
  126.  
  127. System.out.println(localFileName + "\t" + numWritten);
  128. drawLoadingText("Unpacking..");
  129.  
  130. } catch (Exception exception) {
  131. exception.printStackTrace();
  132. } finally {
  133. try {
  134. if (in != null) {
  135. in.close();
  136. }
  137. if (out != null) {
  138. out.close();
  139. }
  140. } catch (IOException ioe) {
  141. }
  142. }
  143.  
  144. }
  145.  
  146. private String getArchivedName() {
  147. int lastSlashIndex = getCacheLink().lastIndexOf('/');
  148. if (lastSlashIndex >= 0
  149. && lastSlashIndex < getCacheLink().length() -1) {
  150. return getCacheLink().substring(lastSlashIndex + 1);
  151. } else {
  152. //System.err.println("error retreiving archivaed name.");
  153. }
  154. return "";
  155. }
  156.  
  157.  
  158.  
  159.  
  160. private void unZip() {
  161.  
  162. try {
  163. InputStream in =
  164. new NovaScape(new FileInputStream(fileToExtract));
  165. ZipInputStream zin = new ZipInputStream(in);
  166. ZipEntry e;
  167.  
  168. while((e=zin.getNextEntry()) != null) {
  169.  
  170. if(e.isDirectory()) {
  171. (new File(getCacheDir() + e.getName())).mkdir();
  172. } else {
  173.  
  174. if (e.getName().equals(fileToExtract)) {
  175. unzip(zin, fileToExtract);
  176. break;
  177. }
  178. unzip(zin, getCacheDir() + e.getName());
  179. }
  180. //System.out.println("unzipping2 " + e.getName());
  181. }
  182. zin.close();
  183.  
  184. } catch(Exception e) {
  185. e.printStackTrace();
  186. }
  187. }
  188.  
  189. private void deleteZIP(String fileName){
  190. // A File object to represent the filename
  191. File f = new File(getCacheDir() + fileName);
  192.  
  193. // Make sure the file or directory exists and isn't write protected
  194. if (!f.exists())
  195. throw new IllegalArgumentException(
  196. "Delete: no such file or directory: " + fileName);
  197.  
  198. if (!f.canWrite())
  199. throw new IllegalArgumentException("Delete: write protected: "
  200. + fileName);
  201.  
  202. // If it is a directory, make sure it is empty
  203. if (f.isDirectory()) {
  204. String[] files = f.list();
  205. if (files.length > 0)
  206. throw new IllegalArgumentException(
  207. "Delete: directory not empty: " + fileName);
  208. }
  209.  
  210. // Attempt to delete it
  211. boolean success = f.delete();
  212.  
  213. if (!success)
  214. throw new IllegalArgumentException("Delete: deletion failed");
  215.  
  216. }
  217.  
  218. private void unzip(ZipInputStream zin, String s)
  219. throws IOException {
  220.  
  221. FileOutputStream out = new FileOutputStream(s);
  222. //System.out.println("unzipping " + s);
  223. byte [] b = new byte[BUFFER];
  224. int len = 0;
  225.  
  226. while ((len = zin.read(b)) != -1) {
  227. out.write(b,0,len);
  228. }
  229. out.close();
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement