How to Store Unix Permissions in a Zip File (Built with Apache Ant)

Can i store unix permissions in a zip file (built with apache ant)?

You cannot store Linux/Unix file permissions in a ZIP file.

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.

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 compression="gzip" destfile="my-archive.tgz">
<tarfileset mode="544" dir="dir_with_shell_scripts">
<include name="*.sh"/>
</tarfileset>
</tar>

Preserve file permissions when unzipping and the zipping files using ant

Turns out that ant will destroy all information on permissions when unzipping due to a restriction in Java. However, what is possible is to add files to an existing zip file which preserves the permissions of the existing files:

<!-- Add to zip -->
<zip destfile="${existingZipFiledirectory}.zip"
basedir="${directoryOfFilesToAdd}"
update="true"
/>

The above script will update the zip file specified with the content in basedir, preserving file permissions in the original zip.

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

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

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...)

File permissions lost after unzipping with zipfile

Python's zipfile does not support Unix-style permissions inside Zip files, since they are non standard. You can just call the unzip command via the subprocess module:

import subprocess

subprocess.run(["unzip", download_file, "-d", script_dir])

How to preserve exec file in ant copy task

As noted in the ant copy task guide:

Unix Note: File permissions are not retained when files are copied; they end up with the default UMASK permissions instead. This is caused by the lack of any means to query or set file permissions in the current Java runtimes. If you need a permission-preserving copy function, use this instead:

<exec executable="cp" ... >

So, in your case, replace <copy> with:

<exec executable="cp">
<arg line="-R old_folder/bundles my_new_folder"/>
</exec>

(note that you should use forward slashes, even if this ant script is being used under Windows).

creating a jar file - preserving file permissions

The jar tool doesn't store file permissions, so you can't recover what has not been stored. jar format is not intended to archive files and metadata as zip or tar, it is a simple container to embed java files for an application.

How do I programmatically change file permissions?

Full control over file attributes is available in Java 7, as part of the "new" New IO facility (NIO.2). For example, POSIX permissions can be set on an existing file with setPosixFilePermissions(), or atomically at file creation with methods like createFile() or newByteChannel().

You can create a set of permissions using EnumSet.of(), but the helper method PosixFilePermissions.fromString() will uses a conventional format that will be more readable to many developers. For APIs that accept a FileAttribute, you can wrap the set of permissions with with PosixFilePermissions.asFileAttribute().

Set<PosixFilePermission> ownerWritable = PosixFilePermissions.fromString("rw-r--r--");
FileAttribute<?> permissions = PosixFilePermissions.asFileAttribute(ownerWritable);
Files.createFile(path, permissions);

In earlier versions of Java, using native code of your own, or exec-ing command-line utilities are common approaches.



Related Topics



Leave a reply



Submit