Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 955
Raymond Kroeker Avatar asked Jun 26 '09 18:06

Raymond Kroeker


People also ask

Does zip preserve file permissions?

zip/unzip in modern linux distros will preserve file permissions. But you need to make sure that the zip in windows honours this. Show activity on this post. If you use a tar file instead of a zip file, it should preserve the permissions for you.

Can Java read a .zip file?

As of Java 7, the NIO АРI provides a better and more generic way of accessing the contents of ZIP or JAR files. Actually, it is now a unified API which allows you to treat ZIP files exactly like normal files. In order to extract all of the files contained inside of a ZIP file in this API, you'd do as shown below.

How can I read the content of a zip file without unzipping it in Java?

Methods. getComment(): String – returns the zip file comment, or null if none. getEntry(String name): ZipEntry – returns the zip file entry for the specified name, or null if not found. getInputStream(ZipEntry entry) : InputStream – Returns an input stream for reading the contents of the specified zip file entry.


1 Answers

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.

like image 128
dm76 Avatar answered Nov 01 '22 22:11

dm76