是否可以使用java FileSystem创建一个新的zip文件? [英] Is it possible to create a NEW zip file using the java FileSystem?
问题描述
我已经使用java 7提供的FileSystem
成功修改了(现有)zip文件的内容,但是当我尝试通过这种方法创建新的zip文件时,它失败了,并显示以下错误消息: "zip END header not found"
,这是合乎逻辑的,因为我执行此操作的方式是,首先创建文件(Files.createFile
),该文件是一个完全空的文件,然后尝试访问其文件系统,并且由于该文件是清空无法在zip文件中找到任何标头的问题,我的问题是可以使用此方法创建完全为空的新zip文件的任何方法吗?我考虑过的黑客是在zip文件中添加一个空的新ZipEntry
,然后使用该新的空文件来创建基于该文件的文件系统,但是我真的想认为oracle的家伙实现了一个更好的(更简便)的方法使用nio和文件系统...
I've successfully modified the contents of a (existing) zip file using the FileSystem
provided 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 is is 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 ZipEntry
to 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...
这是我的代码(创建文件系统时出现错误):
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);
.
.
.
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();
这并不意味着给出的答案是错误的,只是不适用于我的特殊情况
推荐答案
如
这篇关于是否可以使用java FileSystem创建一个新的zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!