java 使用 JDK 5 api 从 zip 文件中提取时保持文件权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1050560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:56:17  来源:igfitidea点击:

Maintain file permissions when extracting from a zip file using JDK 5 api

javazipio

提问by Raymond Kroeker

I am using java.util.Zip and java.util.ZipEntry to successfully extra a zip file's contents to disk. I would like to maintain the file permissions set when extracting on a *nix file-system.

我正在使用 java.util.Zip 和 java.util.ZipEntry 成功地将 zip 文件的内容附加到磁盘。我想在 *nix 文件系统上提取时维护文件权限集。

Can anyone point me to the "correct" way to do this?

任何人都可以指出我这样做的“正确”方法吗?

采纳答案by Patrice M.

Essentially, you can't STORE (unix) file permissions in Zip/Jar files, so you can't preserve them when extracting your jar (they were lost when the jar was created to begin with). See this question: creating a jar file - preserving file permissions

本质上,您无法在 Zip/Jar 文件中存储(unix)文件权限,因此在提取 jar 时无法保留它们(在创建 jar 时它们丢失了)。看到这个问题:创建一个 jar 文件 - 保留文件权限

If you need the file permissions preserved in the archive and restored when extracting, you need to consider the alternative .tar or .tar.gz / .tar.bz2 format, which is also natively supported by most Java build tools (Ant, Gradle, Maven...)

如果您需要保留在存档中并在提取时恢复的文件权限,则需要考虑替代 .tar 或 .tar.gz / .tar.bz2 格式,大多数 Java 构建工具(Ant、Gradle、马文...)

回答by dm76

I think it is actually impossible to keep the permissions correctly.

我认为正确保留权限实际上是不可能的。

Permissions are very OS specific: while POSIX file permissions allow the user to set whether you can read, write or execute a file for the file owner, the group and others, the NTFS file system has a similar system but the concept for an execute permission is inexistant. And the early FAT/FAT32 file syste, do not have file permissions at all (a part from the read-only attribute).

权限是特定于操作系统的:虽然 POSIX 文件权限允许用户为文件所有者、组和其他人设置是否可以读取、写入或执行文件,但 NTFS 文件系统具有类似的系统但执行权限的概念是不存在的。而早期的FAT/FAT32文件系统,根本没有文件权限(一部分来自只读属性)。

Being cross-platform, it would be difficult for java to set the permission properly on the newly created (unzipped) files depending on the underlying OS....

作为跨平台,java 很难根据底层操作系统对新创建(解压缩)的文件正确设置权限......

That said, Java 6 has a new java.io.File classthat allows you to set permissions (with methods like setExecutable(), setReadable(), etc...)

也就是说,Java 6 有一个新的java.io.File 类,它允许您设置权限(使用 setExecutable()、setReadable() 等方法......)

These helped me a lot, especially the setExecutable() which was my main concerned when having to unzip executables on a Linux file system. And you don't have to bother about figuring out what OS you are running on as the method will simply do nothing if running under Windows or other systems without the concept of executable files.

这些对我有很大帮助,尤其是 setExecutable(),当我不得不在 Linux 文件系统上解压缩可执行文件时,我主要关注它。而且您不必费心弄清楚您正在运行的操作系统,因为如果在没有可执行文件概念的 Windows 或其他系统下运行,该方法将什么也不做。

回答by Jon

Look at Apache Commons Compressand look at TarArchiveEntry, that should preserve the file permissions like you want it to.

查看Apache Commons Compress并查看 TarArchiveEntry,它应该像您希望的那样保留文件权限。

TarArchiveEntry entry = tarInput.getNextTarEntry();

Here are the javadocs. I think I've gone Commons mad...

这是javadocs。我想我已经让Commons发疯了......

回答by tkruse

As tracked in this OpenJDK bug: https://bugs.openjdk.java.net/browse/JDK-6194856:

正如在此 OpenJDK 错误中所跟踪的:https: //bugs.openjdk.java.net/browse/JDK-6194856 :

The "standard" zip file format does not have any way to store "executable file" meta-information, since it was originally designed for MS-DOS filesystems.

In order to preserve Unix information like file modes, the zip handling code would have to handle extensions to the zip file format. Other zip implementations have such extensions.

There are some extensions to the file format that would allow maintaining this kind of information, but it can never be guaranteed (there is no requirement that zip files contain such "extended" metadata) and it would be a lot of work to implement and (especially!) test.

“标准”zip 文件格式无法存储“可执行文件”元信息,因为它最初是为 MS-DOS 文件系统设计的。

为了保留像文件模式这样的 Unix 信息,zip 处理代码必须处理 zip 文件格式的扩展。其他 zip 实现有这样的扩展。

文件格式有一些扩展允许维护此类信息,但永远无法保证(不要求 zip 文件包含此类“扩展”元数据),并且实现和(特别是!)测试。

Also see this answer: Can i store unix permissions in a zip file (built with apache ant)?

另请参阅此答案: 我可以将 unix 权限存储在 zip 文件中吗(使用 apache ant 构建)?

Non-standard Java libraries may implement this (such as apache-commons).

非标准 Java 库可能会实现这一点(例如 apache-commons)。