我有一个RuneScape
私人服务器,我有一个客户端。
我正在尝试制作一个 Web 客户端,但由于某种原因,它会从我的网站下载缓存并保留在 cache.zip 中。它不会提取它只是下载到我制作的位置。我很困惑,需要帮助来解决这个问题,因为我向我的成员保证它今晚会上线。
这是下载缓存时文件夹的样子。
假设将那里的文件提取到文件夹中。
这是我的CacheDownloader.java
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 >= 0 && lastSlashIndex < 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();
}
}