解压缩多个文件-Java
我正在为一个游戏客户端开发一个自动更新程序,我遇到了一个问题 我需要它做的是:下载cache.zip和client.zip。将cache.zip解压缩到cacheDir,并将client.zip解压缩到运行jar(游戏)的相同位置解压缩多个文件-Java,java,caching,zip,zipfile,Java,Caching,Zip,Zipfile,我正在为一个游戏客户端开发一个自动更新程序,我遇到了一个问题 我需要它做的是:下载cache.zip和client.zip。将cache.zip解压缩到cacheDir,并将client.zip解压缩到运行jar(游戏)的相同位置 它现在的功能是:下载cache.zip和client.zip。将cache.zip提取到正确的位置,但也提取到jar所在的位置。它根本不解压client.zip。我使用此函数解压文件: public static void unzip(final ZipFile so
它现在的功能是:下载cache.zip和client.zip。将cache.zip提取到正确的位置,但也提取到jar所在的位置。它根本不解压client.zip。我使用此函数解压文件:
public static void unzip(final ZipFile source, final File destination) throws IOException {
for (final ZipEntry entry : Collections.list(source.entries())) {
unzip(source, entry, destination);
}
}
private static void unzip(final ZipFile source, final ZipEntry entry, final File destination) throws IOException {
if (!entry.isDirectory()) {
final File resource = new File(destination, entry.getName());
if (!resource.getCanonicalPath().startsWith(destination.getCanonicalPath() + File.separator)) {
throw new IOException("Entry is outside of the target dir: " + entry);
}
final File folder = resource.getParentFile();
if (!folder.exists()) {
if (!folder.mkdirs()) {
throw new IOException();
}
}
try (final BufferedInputStream input = new BufferedInputStream(source.getInputStream(entry));
final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(resource))) {
output.write(input.readAllBytes());
output.flush();
}
}
}
以下是一些过载:
public static void unzip(final String file) throws IOException {
final File source = new File(file);
unzip(
new ZipFile(source),
new File(source.getParent(), source.getName().substring(0, source.getName().lastIndexOf('.')))
);
}
public static void unzip(final String source, final String destination) throws IOException {
unzip(new File(source), new File(destination));
}
public static void unzip(final File source, final File destination) throws IOException {
unzip(new ZipFile(source), destination);
}
注:
- 它忽略空目录。如果需要,您可以在条目中添加else和mkdir
- 如果需要加快进程,可以在
上添加一个线程池,用于调用unzip(最终ZipFile源,最终文件目标)
,并使用多个线程unzip(最终ZipFile源,最终ZipEntry条目,最终文件目标)
正在检查每个条目的输出规范路径是否以目标的规范路径开始,以避免出现解压(最终ZipFile源、最终ZipPentry条目、最终文件目标)
-请参阅-但您可以忽略此检查Zip-Slip漏洞
String fileRepertory = Setup.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
最后,如果您需要压缩一个文件(我在这里发布它作为我自己的备份^^ ^'))
publicstaticvoidzip(最终文件目的地,最终列表toZip)抛出IOException{
try(final-ZipOutputStream zip=new-zipoutstream(new-FileOutputStream(destination))){
for(最终文件:toZip){
final ZipEntry entry=new ZipEntry(file.getCanonicalPath());
邮政编码putNextEntry(条目);
write(Files.readAllBytes(file.toPath());
}
}
}
谢谢你,我能够将我的解压结果建立在这个基础上并解决我的问题!你是炸弹readAllBytes
不是很好,最好使用input.transferTo(output)
。
public static void zip(final File destination, final List<File> toZip) throws IOException {
try (final ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(destination))) {
for (final File file : toZip) {
final ZipEntry entry = new ZipEntry(file.getCanonicalPath());
zip.putNextEntry(entry);
zip.write(Files.readAllBytes(file.toPath()));
}
}
}