我正在为游戏客户端制作一个自动更新程序,但我遇到了一个问题。
我需要它做什么:下载cache.zip和client.zip。将cache.zip解压缩到cacheDir并将client.zip解压缩到运行jar(游戏)的相同位置。
它现在做什么:下载cache.zip和client.zip。将cache.zip提取到正确的位置,但也提取到jar所在的位置。它根本不提取client.zip。
答案 0 :(得分:0)
我使用此函数解压缩文件:
private void unzip(String zipFile) throws Exception
{
int BUFFER = 2048;
File file = new File(zipFile);
@SuppressWarnings("resource")
ZipFile zip = new ZipFile(file);
String newPath = zipFile.substring(0, zipFile.length() - 4);
new File(newPath).mkdir();
@SuppressWarnings("rawtypes")
Enumeration zipFileEntries = zip.entries();
while (zipFileEntries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
File destinationParent = destFile.getParentFile();
destinationParent.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
int currentByte;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
while ((currentByte = is.read(data, 0, BUFFER)) != -1)
{
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
}
如果你需要获得你的jar剧目:
String fileRepertory = Setup.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();