Guest User

cachedownloader.java

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