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

是否可以使用java文件系统创建新的zip文件?,java,file,filesystems,zip,Java,File,Filesystems,Zip,我已经使用java 7提供的文件系统成功地修改了(现有)zip文件的内容,但是当我尝试用这种方法创建一个新的zip文件时失败了,错误消息是:“zip END header not found”,这是合乎逻辑的,因为我这样做,首先我创建了文件(Files.createFile)这是一个完全空的文件,然后我尝试访问它的文件系统,因为文件是空的,在zip中找不到任何头,我的问题是有没有办法用这种方法创建一个完全空的新zip文件?;我考虑过的黑客方法是在zip文件,然后使用新的空文件来包装基于它的文件系

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

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

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

回答: 在我的特殊情况下,由于路径中的空格,给出的答案不适用,因此我必须以我不希望的方式进行:

Files.createFile(zipLocation);
ZipOutputStream out = new ZipOutputStream(
    new FileOutputStream(zipLocation.toFile()));
out.putNextEntry(new ZipEntry(""));
out.closeEntry();
out.close();
这并不意味着给出的答案是错误的,它只是不适用于我的特定情况

,如所述:

publicstaticvoidcreatezip(路径zipLocation,路径tobeaded,字符串internalPath)抛出Throwable{
Map env=new HashMap();
//检查文件是否存在
put(“create”,String.valueOf(Files.notExists(zipLocation));
//使用Zip文件系统URI
URI fileUri=zipLocation.toUri();//此处
URI zipUri=新URI(“jar:+fileUri.getScheme(),fileUri.getPath(),null);
系统输出打印(zipUri);
//URI=URI.create(“jar:file:+zipLocation);//这里创建
//拉链
//试用资源
try(FileSystem-zipfs=FileSystems.newFileSystem(zipUri,env)){
//在zipfs中创建内部路径
Path internalTargetPath=zipfs.getPath(internalPath);
//创建父目录
Files.createDirectories(internalTargetPath.getParent());
//将文件复制到zip文件中
复制(toheaded、internalTargetPath、StandardCopyOption.REPLACE_EXISTING);
}
}
公共静态void main(字符串[]args)抛出可丢弃的{
Path zipLocation=FileSystems.getDefault().getPath(“a.zip”).toAbsolutionPath();
Path TOBEADED=FileSystems.getDefault().getPath(“a.txt”).toabsolutionPath();
createZip(zipLocation,标题为“aa/aa.txt”);
}

就快到了,但我在URI部分遇到了问题,(不幸的是)我在windows中工作,在创建URI时它会给我错误,无论“:”、“\”或者空格,有什么建议吗?@Ordiel编辑了答案。结果是不正确的。也应该处理windows。如果zip路径中有空格,它会失败…:/你认为可能是什么?遗憾的是,jre上似乎有一个bug。请参阅以获取深入检查。我不得不对URI进行一些黑客攻击,其名称为:FileSystems.newFileSystem(URI.create(“jar:file:+path.get(zipFilePath.toUri().getPath()))、env、null)“no-create”答案非常有效。令人惊讶的是。
Files.createFile(zipLocation);
ZipOutputStream out = new ZipOutputStream(
    new FileOutputStream(zipLocation.toFile()));
out.putNextEntry(new ZipEntry(""));
out.closeEntry();
out.close();
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");
}