java 我可以将 unix 权限存储在 zip 文件中吗(用 apache ant 构建)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13633488/
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
Can i store unix permissions in a zip file (built with apache ant)?
提问by Paul Taylor
I'm building a linux installer for a Java application, and I end up with an install.jar and a setup.sh that I put in a zip file using ant.
我正在为 Java 应用程序构建一个 linux 安装程序,最终得到一个 install.jar 和一个 setup.sh,我使用 ant 将它们放入一个 zip 文件中。
The idea is the user unzips the zip file and then runs setup.sh but the trouble is that they always need to chmod setup.sh first to give themselves execute permissions.
这个想法是用户解压缩 zip 文件,然后运行 setup.sh 但问题是他们总是需要先 chmod setup.sh 给自己执行权限。
I want to remove this step and Im not sure if the problem part is:
我想删除这一步,我不确定问题部分是否是:
- That Im building on Windows
- That Im building with ant zip task
- Or that zips cannot presreve permissions and will always unzip without x permissions.
- 我在 Windows 上构建的
- 我用 ant zip 任务构建的那个
- 或者 zips 无法预先保留权限,并且将始终在没有 x 权限的情况下解压缩。
回答by Blaine
You don't have to switch to tar files. I don't know why people who don't know Ant are offering advice on this topic.
您不必切换到 tar 文件。我不知道为什么不了解 Ant 的人会提供有关此主题的建议。
Use zipfileset's filemode parameter. Documented at http://ant.apache.org/manual/Types/zipfileset.html
使用 zipfileset 的 filemode 参数。记录在http://ant.apache.org/manual/Types/zipfileset.html
回答by a_horse_with_no_name
You cannot store Linux/Unix file permissions in a ZIP file.
您不能在 ZIP 文件中存储 Linux/Unix 文件权限。
Edit (after comments)by using the "external attributes" field inside the ZIP header these attributes can be store inside a ZIP file. GNU's unzip
is apparently able to read that additional field and restore file permissions. I'm not sure when this was added to the ZIP format as the early versions - coming from a MS-DOS world - did not have support for this.
通过使用 ZIP 标头内的“外部属性”字段编辑(在评论后)这些属性可以存储在 ZIP 文件中。GNUunzip
显然能够读取该附加字段并恢复文件权限。我不确定这是什么时候添加到 ZIP 格式的,因为早期版本 - 来自 MS-DOS 世界 - 不支持这个。
The TAR format - being a "native" Unix/Linux format - has been designed to include file attributes and Ant can create TAR files that will preserve attributes across all Linux/Unix operating systems.
TAR 格式 - 作为“原生”Unix/Linux 格式 - 旨在包含文件属性,Ant 可以创建 TAR 文件,这些文件将在所有 Linux/Unix 操作系统中保留属性。
<tar compression="gzip" destfile="my-archive.tgz"> <tarfileset mode="544" dir="dir_with_shell_scripts"> <include name="*.sh"/> </tarfileset> </tar>
回答by mprivat
I think you can do this with Apache Commons Compress.
我认为您可以使用Apache Commons Compress来做到这一点。
First paragraph:
第一段:
Access to internal and external attributes (which are used to store Unix permission by some zip implementations).
访问内部和外部属性(用于存储某些 zip 实现的 Unix 权限)。
Take a look at the APIand look for setUnixMode()
看一看API并寻找setUnixMode()
回答by Humpity
zip/unzip in modern linux distros will preserve file permissions. But you need to make sure that the zip in windows honours this.
现代 linux 发行版中的 zip/unzip 将保留文件权限。但是您需要确保 Windows 中的 zip 遵守这一点。
回答by VGR
To expand on Blaine's answer, you can use <zipfileset>child elements to specify permissions:
要扩展 Blaine 的答案,您可以使用<zipfileset>子元素来指定权限:
<zip destfile="build/MyApplication.zip" encoding="UTF-8">
<zipfileset dir="${content-dir}" encoding="UTF-8"
includes="**/setup.sh" filemode="755"/>
<zipfileset dir="${content-dir}" encoding="UTF-8"
excludes="**/setup.sh"/>
</zip>
回答by dj_segfault
If you use a tar file instead of a zip file, it should preserve the permissions for you. The files' owners and groups might not match up, but the permission bits will.
如果您使用 tar 文件而不是 zip 文件,它应该为您保留权限。文件的所有者和组可能不匹配,但权限位会匹配。
回答by Varun Bhatia
<shellscript shell="sh" dir="abc" failonerror="true">
zip -ry abc.zip *
</shellscript>
The ant script above invokes a shell script to create the zip archive in which the permissions are very well preserved. It works with Ant 1.7 and above. However I have not tried verified running this ant script on windows. This works very well on mac.
上面的 ant 脚本调用一个 shell 脚本来创建 zip 存档,其中的权限被很好地保留了下来。它适用于 Ant 1.7 及更高版本。但是我还没有尝试在 Windows 上验证运行这个 ant 脚本。这在 mac 上非常有效。