ZipEntry到文件

JF *_*ier 4 java zip file

有没有直接的方法解开java.util.zip.ZipEntry一个File

我想指定一个位置(如"C:\ temp\myfile.java")并将Entry解压缩到该位置.

网上有一些带流的代码,但我更喜欢经过测试的库函数.

Evg*_*eev 20

使用ZipFile类

    ZipFile zf = new ZipFile("zipfile");
Run Code Online (Sandbox Code Playgroud)

进入

    ZipEntry e = zf.getEntry("name");
Run Code Online (Sandbox Code Playgroud)

得到inpustream

    InputStream is = zf.getInputStream(e);
Run Code Online (Sandbox Code Playgroud)

保存字节

    Files.copy(is, Paths.get("C:\\temp\\myfile.java"));
Run Code Online (Sandbox Code Playgroud)


Sat*_*hia 6

使用下面的代码将“zip 文件”提取到文件中,然后使用 .zip 文件添加到列表中ZipEntry。希望这会对您有所帮助。

private List<File> unzip(Resource resource) {
    List<File> files = new ArrayList<>();
    try {
        ZipInputStream zin = new ZipInputStream(resource.getInputStream());
        ZipEntry entry = null;
        while((entry = zin.getNextEntry()) != null) {
            File file = new File(entry.getName());
            FileOutputStream  os = new FileOutputStream(file);
            for (int c = zin.read(); c != -1; c = zin.read()) {
                os.write(c);
            }
            os.close();
            files.add(file);
        }
    } catch (IOException e) {
        log.error("Error while extract the zip: "+e);
    }
    return files;
}
Run Code Online (Sandbox Code Playgroud)