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

后端 未结 4 1611
深忆病人
深忆病人 2020-12-06 16:15

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

相关标签:
4条回答
  • 2020-12-06 16:44

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

    0 讨论(0)
  • 2020-12-06 16:46

    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.

    0 讨论(0)
  • 2020-12-06 16:55

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

    0 讨论(0)
  • 2020-12-06 16:58

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

    0 讨论(0)
提交回复
热议问题