所以我使用的代码非常类似于以下内容(从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”文件权限,并且已成功创建目标。解压缩结果时,相应的文件仅具有“ 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不支持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权限
  命令。

09-10 10:01
查看更多