Java ZipFileSystem 属性
Java ZipFileSystem attributes
所以我使用的代码非常类似于以下内容(来自 http://fahdshariff.blogspot.com/2011/08/java-7-working-with-zip-files.html):
/**
* Creates/updates a zip file.
* @param zipFilename the name of the zip to create
* @param filenames list of filename to add to the zip
* @throws IOException
*/
public static void create(String zipFilename, String... filenames)
throws IOException {
try (FileSystem zipFileSystem = createZipFileSystem(zipFilename, true)) {
final Path root = zipFileSystem.getPath("/");
//iterate over the files we need to add
for (String filename : filenames) {
final Path src = Paths.get(filename);
//add a file to the zip file system
if(!Files.isDirectory(src)){
final Path dest = zipFileSystem.getPath(root.toString(),
src.toString());
final Path parent = dest.getParent();
if(Files.notExists(parent)){
System.out.printf("Creating directory %s\n", parent);
Files.createDirectories(parent);
}
Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
}
else{
//for directories, walk the file tree
Files.walkFileTree(src, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
final Path dest = zipFileSystem.getPath(root.toString(),
file.toString());
Files.copy(file, dest, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
final Path dirToCreate = zipFileSystem.getPath(root.toString(),
dir.toString());
if(Files.notExists(dirToCreate)){
System.out.printf("Creating directory %s\n", dirToCreate);
Files.createDirectories(dirToCreate);
}
return FileVisitResult.CONTINUE;
}
});
}
}
}
}
当我在visitFile()方法中调用Files.copy()时,src有'rwxr-xr-x'文件权限,dest创建成功。当我解压缩结果时,对应的文件只有 'rw-r--r--' 权限。我尝试使用
Files.copy(src, dest, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING)
但文件仍然具有 'rw-r--r--' 权限...使用 'rwxr-xr-x' 权限集执行 Files.create() 具有相同的结果...有什么想法吗?
编辑:我尝试了以下方法,但我只得到一个 UnsupportedOperationException:
Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rwxr-xr-x");
Files.setPosixFilePermissions(dest, permissions);
ZIP file format by default does not support Unix filesystem features. And the ZIP filesystem provider至少不支持存储Unix文件权限。
// ZIP created with teh Java zipfs
zipinfo foobar.zip
file system or operating system of origin: MS-DOS, OS/2 or NT FAT
version of encoding software: 2.0
...
non-MSDOS external file attributes: 000000 hex
MS-DOS file attributes (00 hex): none
如果您使用 Linux 版本的 Info-ZIP 实现创建 ZIP 文件。
file system or operating system of origin: Unix
version of encoding software: 3.0
...
apparent file type: text
Unix file attributes (100755 octal): -rwxr-xr-x
MS-DOS file attributes (00 hex): none
有关更多信息,您可以查看 ZipFileSystem.java
original source or from the current included in JDK 8 Demos and Samples
的来源
编辑 您可以尝试 TrueVFS 而不是 zipfs
。快速检查后,它似乎也不支持 Unix 文件权限。
您可以使用 Apache commons compress。按照 javadoc 它支持 Unix 权限。
来自 ZipArchiveEntry.setUnixMode
Sets Unix permissions in a way that is understood by Info-Zip's unzip
command.
所以我使用的代码非常类似于以下内容(来自 http://fahdshariff.blogspot.com/2011/08/java-7-working-with-zip-files.html):
/**
* Creates/updates a zip file.
* @param zipFilename the name of the zip to create
* @param filenames list of filename to add to the zip
* @throws IOException
*/
public static void create(String zipFilename, String... filenames)
throws IOException {
try (FileSystem zipFileSystem = createZipFileSystem(zipFilename, true)) {
final Path root = zipFileSystem.getPath("/");
//iterate over the files we need to add
for (String filename : filenames) {
final Path src = Paths.get(filename);
//add a file to the zip file system
if(!Files.isDirectory(src)){
final Path dest = zipFileSystem.getPath(root.toString(),
src.toString());
final Path parent = dest.getParent();
if(Files.notExists(parent)){
System.out.printf("Creating directory %s\n", parent);
Files.createDirectories(parent);
}
Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
}
else{
//for directories, walk the file tree
Files.walkFileTree(src, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
final Path dest = zipFileSystem.getPath(root.toString(),
file.toString());
Files.copy(file, dest, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
final Path dirToCreate = zipFileSystem.getPath(root.toString(),
dir.toString());
if(Files.notExists(dirToCreate)){
System.out.printf("Creating directory %s\n", dirToCreate);
Files.createDirectories(dirToCreate);
}
return FileVisitResult.CONTINUE;
}
});
}
}
}
}
当我在visitFile()方法中调用Files.copy()时,src有'rwxr-xr-x'文件权限,dest创建成功。当我解压缩结果时,对应的文件只有 'rw-r--r--' 权限。我尝试使用
Files.copy(src, dest, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING)
但文件仍然具有 'rw-r--r--' 权限...使用 'rwxr-xr-x' 权限集执行 Files.create() 具有相同的结果...有什么想法吗?
编辑:我尝试了以下方法,但我只得到一个 UnsupportedOperationException:
Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rwxr-xr-x");
Files.setPosixFilePermissions(dest, permissions);
ZIP file format by default does not support Unix filesystem features. And the ZIP filesystem provider至少不支持存储Unix文件权限。
// ZIP created with teh Java zipfs
zipinfo foobar.zip
file system or operating system of origin: MS-DOS, OS/2 or NT FAT
version of encoding software: 2.0
...
non-MSDOS external file attributes: 000000 hex
MS-DOS file attributes (00 hex): none
如果您使用 Linux 版本的 Info-ZIP 实现创建 ZIP 文件。
file system or operating system of origin: Unix
version of encoding software: 3.0
...
apparent file type: text
Unix file attributes (100755 octal): -rwxr-xr-x
MS-DOS file attributes (00 hex): none
有关更多信息,您可以查看 ZipFileSystem.java
original source or from the current included in JDK 8 Demos and Samples
编辑 您可以尝试 TrueVFS 而不是 zipfs
。快速检查后,它似乎也不支持 Unix 文件权限。
您可以使用 Apache commons compress。按照 javadoc 它支持 Unix 权限。
来自 ZipArchiveEntry.setUnixMode
Sets Unix permissions in a way that is understood by Info-Zip's unzip command.