解压缩多个文件-Java [英] Unzipping Multiple Files - Java

查看:61
本文介绍了解压缩多个文件-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏客户端开发自动更新程序,但遇到了问题.

I'm working on an auto updater for a game client and I've run into an issue.

我需要执行的操作:下载cache.zip和client.zip.将cache.zip提取到cacheDir,将client.zip提取到运行jar(游戏)的位置.

What I need it to do: Download cache.zip and client.zip. Extract cache.zip to cacheDir and extract client.zip to the same location they are running the jar(game).

它现在做什么:下载cache.zip和client.zip.将cache.zip提取到正确的位置,也提取到jar所在的位置.它根本不提取client.zip.

What it does right now: Downloads both cache.zip and client.zip. Extracts cache.zip to the correct location, but also to the location the jar is at. It doesn't extract client.zip at all.

推荐答案

我使用此功能将文件解压缩:

I use this function to unzip a file:

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条目.

  • It ignore the empty directories. You can add an else and mkdir the entries if you need them.

如果需要加快处理速度,可以在 unzip(最终ZipFile源,最终的文件目标)上添加线程池,以调用 unzip(最终ZipFile源,最终的ZipEntry).条目,最终文件目标)(具有多个线程).

If you need to speedup the process, you can add a threadpool on unzip(final ZipFile source, final File destination) for calling unzip(final ZipFile source, final ZipEntry entry, final File destination) with multiple threads.

unzip(最终的ZipFile源,最终的ZipEntry条目,最终的文件目标)正在检查每个条目是否输出的规范路径以目标的规范路径开头,以避免 Zip滑动漏洞-请参见 https://snyk.io/research/zip-slip-vulnerability -但您可能会忽略用例的检查.

unzip(final ZipFile source, final ZipEntry entry, final File destination) is checking for each entry if the canonical path of the output start by the canonical path of the destination to avoid Zip Slip Vulnerability - See https://snyk.io/research/zip-slip-vulnerability - but you may ignore this check for you use case.

如果需要保存罐子清单,

And if you need to get your jar repertory:

String fileRepertory = Setup.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();

最后,如果您需要压缩文件(我将其作为备份备份到这里^^')

And finally, if you need to zip a file (I post it here as a backup for myself ^^')

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()));
        }
    }
}

这篇关于解压缩多个文件-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆