title: RuneScape Private Server - Cache Downloader
body: <p>I do have a <code>RuneScape</code> Private server and I do have a client.</p>
  
  <p>I'm trying to make a webclient, which I have done but for some reason it downloads the cache from my website and stays in the cache.zip. It doesn't extract it just downloads to the location I've made. I'm stumped and need help to fix this because I promised my members that it would be online tonight.</p>
  
  <p>Here is what the folder looks like when the cache is downloaded.</p>
  
  <p><img src="https://i.stack.imgur.com/5N4lF.png" alt="http://i50.tinypic.com/2iasgmw.png"></p>
  
  <p>It is suppose to extract the files in there to folder.</p>
  
  <p>Here is my <code>CacheDownloader.java</code></p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.FileWriter;
  import java.io.BufferedWriter;
  import java.io.BufferedOutputStream;
  import java.io.BufferedInputStream;
  import java.io.FileOutputStream;
  import java.io.FileInputStream;
  import java.io.InputStream;
  import java.net.URLConnection;
  import java.net.URL;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipInputStream;
  import sign.Signlink;
  
  public class CacheDownloader {
  
      private Client Client;
  
      private final int BUFFER = 1024;
  
      private final int VERSION = 1;
      private String cacheLink = "http://www.survivalpkz.com/client/cache.zip";
      private String fileToExtract = getCacheDir() + getArchivedName();
  
      public CacheDownloader(Client Client) {
          this.Client = Client;
      }
  
      private void drawLoadingText(String text) {
          Client.drawLoadingText(35, text);
      }
  
      private void drawLoadingText(int amount, String text) {
          Client.drawLoadingText(amount, text);
      }
  
      private String getCacheDir() {
          return Signlink.findCacheDir();
      }
  
      private String getCacheLink() {
          return cacheLink;
      }
  
      private int getCacheVersion() {
          return VERSION;
      }
  
      public CacheDownloader downloadCache() {
          try {
              File location = new File(getCacheDir());
              File version = new File(getCacheDir() + "/cacheVersion"
                      + getCacheVersion() + ".dat");
  
              if (!location.exists()) {
                  downloadFile(getCacheLink(), getArchivedName());
  
                  System.out.println("Unzipping the Cache.");
                  unZip();
                  System.out.println("Cache has Unzipped.");
  
                  BufferedWriter versionFile = new BufferedWriter(new FileWriter(
                          getCacheDir() + "/cacheVersion" + getCacheVersion()
                          + ".dat"));
                  versionFile.close();
              } else {
                  if (!version.exists()) {
                      downloadFile(getCacheLink(), getArchivedName());
  
                      System.out.println("Unzipping the Cache.");
                      unZip();
                      System.out.println("Cache has Unzipped.");
  
                      BufferedWriter versionFile = new BufferedWriter(
                              new FileWriter(getCacheDir() + "/cacheVersion"
                                      + getCacheVersion() + ".dat"));
                      versionFile.close();
  
                  } else {
                      return null;
                  }
              }
          } catch (Exception e) {
  
          }
          return null;
      }
  
      private void downloadFile(String address, String localFileName) {
          OutputStream out = null;
          URLConnection conn;
          InputStream in = null;
  
          try {
  
              URL url = new URL(address);
              out = new BufferedOutputStream(new FileOutputStream(getCacheDir()
                      + "/" + localFileName));
  
              conn = url.openConnection();
              in = conn.getInputStream();
  
              byte[] data = new byte[BUFFER];
  
              int numRead;
              long numWritten = 0;
              int length = conn.getContentLength();
  
              while ((numRead = in.read(data)) != -1) {
                  out.write(data, 0, numRead);
                  numWritten += numRead;
  
                  int percentage = (int) (((double) numWritten / (double) length) * 100D);
                  drawLoadingText(percentage, "Downloading Cache " + percentage
                          + "%");
  
              }
  
              System.out.println(localFileName + "\t" + numWritten);
              drawLoadingText("Finished downloading " + getArchivedName() + "!");
  
          } catch (Exception exception) {
              exception.printStackTrace();
          } finally {
              try {
                  if (in != null) {
                      in.close();
                  }
                  if (out != null) {
                      out.close();
                  }
              } catch (IOException ioe) {
              }
          }
  
      }
  
      private String getArchivedName() {
          int lastSlashIndex = getCacheLink().lastIndexOf('/');
          if (lastSlashIndex &gt;= 0 &amp;&amp; lastSlashIndex &lt; getCacheLink().length() - 1) {
              return getCacheLink().substring(lastSlashIndex + 1);
          } else {
              System.err.println("Error retreving Archive.");
          }
          return "";
      }
  
      private void unZip() {
  
          try {
              InputStream in = new BufferedInputStream(new FileInputStream(
                      fileToExtract));
              ZipInputStream zin = new ZipInputStream(in);
              ZipEntry e;
  
              while ((e = zin.getNextEntry()) != null) {
  
                  if (e.isDirectory()) {
                      (new File(getCacheDir() + e.getName())).mkdir();
                  } else {
  
                      if (e.getName().equals(fileToExtract)) {
                          unzip(zin, fileToExtract);
                          break;
                      }
                      unzip(zin, getCacheDir() + e.getName());
                  }
                  System.out.println("Unzipping: " + e.getName());
              }
              zin.close();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      private void unzip(ZipInputStream zin, String s) throws IOException {
  
          FileOutputStream out = new FileOutputStream(s);
          byte[] b = new byte[BUFFER];
          int len = 0;
  
          while ((len = zin.read(b)) != -1) {
              out.write(b, 0, len);
          }
          out.close();
      }
  }
  </code></pre>