是否可以使用 java FileSystem 创建新的 zip 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14733496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:11:51  来源:igfitidea点击:

Is it possible to create a NEW zip file using the java FileSystem?

javafilefilesystemszip

提问by Ordiel

I've successfully modified the contents of a (existing) zip file using the FileSystemprovided by java 7, but when I tried to create a NEW zip file by this method it fails, with the error message that says: "zip END header not found", it is logical because of the way I'm doing it, first I create the file (Files.createFile) which is a completely empty file, and then I try to access to its file system , and since the file is empty its impossible to find any header inside the zip, my question isis there any way to create a new zip file completely empty using this method?; the hack that I've considered is adding an empty new ZipEntryto a the zip file and then using that new empty file to crate the file system based on it, but i really want to think that the guys of oracle implemented a better (easier) way to do this with nio and the filesystems...

我已经使用FileSystemjava 7 提供的成功修改了(现有)zip 文件的内容,但是当我尝试通过这种方法创建一个新的 zip 文件时,它失败了,错误消息是:"zip END header not found",这是合乎逻辑的,因为我这样做的方式是,首先我创建了Files.createFile一个完全空文件的文件 ( ),然后我尝试访问它的文件系统,由于该文件是空的,因此无法在 zip 中找到任何标题,我的问题是有没有办法使用这种方法创建一个完全空的新 zip 文件?我考虑过的黑客是添加一个空的新ZipEntry到一个 zip 文件,然后使用这个新的空文件来创建基于它的文件系统,但我真的想认为 oracle 的人用 nio 和文件系统实现了一种更好(更简单)的方法来做到这一点......

this is my code (the error appears when creating the file system):

这是我的代码(创建文件系统时出现错误):

if (!zipLocation.toFile().exists()) {
        if (creatingFile) {
            Files.createFile(zipLocation);
        }else {
            return false;
        }
    } else if (zipLocation.toFile().exists() && !replacing) {
        return false;
    } 
    final FileSystem fs = FileSystems.newFileSystem(zipLocation, null);
.
.
.

zipLocationis a Path creatingFileis a boolean

zipLocation是一个路径 creatingFile是一个布尔值

ANSWER:in my particular case the answer given didn't work appropriately because of the spaces in the path, therefore i have to do it the way i didn't want to:

答案:在我的特殊情况下,由于路径中的空格,给出的答案无法正常工作,因此我必须按照我不想的方式进行:

Files.createFile(zipLocation);
ZipOutputStream out = new ZipOutputStream(
    new FileOutputStream(zipLocation.toFile()));
out.putNextEntry(new ZipEntry(""));
out.closeEntry();
out.close();

it does not mean that the given answer is wrong, it just didn't work for my particular case

这并不意味着给出的答案是错误的,只是对我的特定情况不起作用

回答by Carlo Pellegrini

As described in The Oracle Site:

Oracle 站点中所述

public static void createZip(Path zipLocation, Path toBeAdded, String internalPath) throws Throwable {
    Map<String, String> env = new HashMap<String, String>();
    // check if file exists
    env.put("create", String.valueOf(Files.notExists(zipLocation)));
    // use a Zip filesystem URI
    URI fileUri = zipLocation.toUri(); // here
    URI zipUri = new URI("jar:" + fileUri.getScheme(), fileUri.getPath(), null);
    System.out.println(zipUri);
    // URI uri = URI.create("jar:file:"+zipLocation); // here creates the
    // zip
    // try with resource
    try (FileSystem zipfs = FileSystems.newFileSystem(zipUri, env)) {
        // Create internal path in the zipfs
        Path internalTargetPath = zipfs.getPath(internalPath);
        // Create parent directory
        Files.createDirectories(internalTargetPath.getParent());
        // copy a file into the zip file
        Files.copy(toBeAdded, internalTargetPath, StandardCopyOption.REPLACE_EXISTING);
    }
}

public static void main(String[] args) throws Throwable {
    Path zipLocation = FileSystems.getDefault().getPath("a.zip").toAbsolutePath();
    Path toBeAdded = FileSystems.getDefault().getPath("a.txt").toAbsolutePath();
    createZip(zipLocation, toBeAdded, "aa/aa.txt");
}