Java ZipFileSystem属性

时间:2016-03-07 07:21:37

标签: java copy filesystems zip file-permissions

所以我使用的代码非常类似于以下内容(从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()具有相同的结果......任何想法?< / p>

编辑:我尝试了以下内容,但我得到了一个UnsupportedOperationException:

Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rwxr-xr-x");
Files.setPosixFilePermissions(dest, permissions);

1 个答案:

答案 0 :(得分:1)

默认情况下,ZIP file format不支持Unix文件系统功能。并且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的来源或JDK 8 Demos and Samples中包含的当前内容

修改 您可以尝试使用TrueVFS代替zipfs。经过快速检查后,它似乎也不支持Unix文件权限。

您可以使用Apache commons compress。在javadoc之后它支持Unix权限。

来自ZipArchiveEntry.setUnixMode

  

以Info-Zip解压缩的方式设置Unix权限   命令。