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

java zip directory apache commons
zip preserve permissions
java zip library
apache commons zip
apache commons compress
java zip encryption
commons compress unzip
java zip central directory

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.

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

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

extract � Zip � Java I/O Q&A, Maintain file permissions when extracting from a zip file using JDK 5 api stackoverflow.com. I am using java.util.Zip and java.util.ZipEntry to successfully extra a� Zip can store Unix file permissions by using the "external attributes" field inside the ZIP header. If it is uncompressed with the right tool on a unix system, the files will keep the permissions. If it is upacked on windows, the files will lose the permissions.

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

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

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

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.

java, java - Maintain file permissions when extracting from a zip file using JDK 5 api - in zip / jar files, so that you can not keep them safe while extracting your jar� Writing the zip file succeeds, but when I open the resulting zip file with either Windows' built-in compress Maintain file permissions when extracting from a zip file using JDK 5 API I am using java.util.Zip and java.util.ZipEntry to successfully extra a zip file's contents to disk.

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

TarArchiveEntry entry = tarInput.getNextTarEntry();

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

[JDK-8148402] Enhance JAR to preserve file permissions (improves , Enhance JAR to preserve file permissions (improves usage in Linux maintain- file-permissions-when-extracting-from-a-zip-file-using-jdk-5-api This article is about how to write a utility class for extracting files and directories in a compressed zip archive, using built-in Java API. The java.util.zip . package provides the following classes for extracting files and directories from a ZIP archive: ZipInputStream: this is the main class which can be used for reading zip file and

As tracked in this OpenJDK bug: 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.

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

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

Java Zip File Folder Example, Java Zip File, Java zip folder, java zip example program, java compress directory, We will also compress a folder and create zip file using java program. ZipEntry we need to keep only relative file path, so we used substring on absolute path to unzip my “DICOM.zip” so that it can extract the all file with same hirarchy as� In Java, file permissions are very OS specific: *nix , NTFS (windows) and FAT/FAT32, all have different kind of file permissions. Java comes with some generic file permission to deal with it. Check if the file permission allow: file.canExecute(); – return true, file is executable; false is not.

JDK Self-Extracting 64-bit Installation, JDK Self-Extracting 64-bit Installation. JDK Self-Extracting Installation for Linux (64-bit) To install, download this file and use the following instructions. your home directory, or a subdirectory that you have permission to write to. See the Preferences API documentation for more information about preferences in the� 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 SE 5.0 Advanced and Java SE 5.0 Support, disabledAlgorithms property in the java.security file or by dynamically setting this Security property to "true" before JSSE is List in jdk5 should be the same to that in JDK6,7 and 8 Zipfile implementation (on Solaris and Linux platforms). Legacy code may use the JDK internal API SerializerFactory to create a Serializer . If you set the umask to 022 then you'll get directories with mode 755. The second problem, that of setting the execute bit on binary executables, has been worked around in the Java Plug-In (4364782). Enhancing the jar tool to preserve file permissions will, as previously stated, require an improved core filesystem API.

Installation of the JDK on Linux Platforms, Installing the JDK on Linux from Archive Binaries and RPM Packages JDK) then you must log in with root credentials to gain the necessary permissions. Ensure that you rename the old directory if it contains files that you would like to keep. file (also called a tarball) is a file that can be uncompressed and extracted in a� Synopsis: Improve jar file handling. Starting with JDK 5.0u85 release, the jar tool no longer allows the leading slash "/" and ".." (dot-dot) path component in zip entry file name when creating new and/or extracting from zip and jar file.

Comments
  • I know this is an rather old post but have you solved that issue ? I'm having the same problem right now ; It only seems to affect an executable file so far... when I unzip it "by hand" the permission is correct
  • I have not in fact solved it. My last approach vector was going to be to pull apart apache's ant task for manipulating zip files and determine what they are doing.
  • Ant can do that only since 1.8, using the new feature of Java.io.File in java 6 (see my answer below)
  • This is not entirely accurate. Extra fields are commonly used for the purposes of file permission preservation.
  • I know this thread is getting quite old but your answer helped me. I was trying to extract a ***.app on Mac Os but the resulting application would launch, I changed permissions of the actual launcher file inside it using the setExecutable() method and it seems to be working perfectly ! Thanks for the help :)
  • does TarArchiveEntry work on zips as well as tar files? they are not the same, right?
  • I agree with Cheeso - what does this have to do with zip files ?
  • There is also ZipArchiveEntry commons.apache.org/proper/commons-compress/zip.html