I'm trying to unarchive tar.gz file using Apache Compress library (Java).

How can I preserve files permissions using Apache Compress library when unarchiving tar.gz?


You are providing too few details, so I am making assumptions on my answer.

There could be a number of reasons for that not to work as expected.

  1. The user-id and group-id mentioned in the tar archive don't exist on the target machine.

  2. The target (sub) file system where the archive is being unarchived don't allow for those "permissions" to stick with the original ones. This is the case with non-Unix file systems like vfat and ntfs as used for target directories and files.

  3. Normal users cannot in general do that. A non-root user cannot transfer the ownership of files and directories to other users.

  4. Some of the target directory aren't real directories (like /proc and /sys), so the restore of files therein won't work.

I am not sure how the "Apache Compress library" works, but when I need to do a similar task I untar the archive with root permissions while using the very same path as the destination of the unarchiving. However Apache Compress library works, if it can handle tar.gz files it needs to be able to accept a number of options similar to those of the tar tool. Which I would definitely use.

Something like:

sudo tar xf mytar.tar.gz -C /

will ensure that any file that's been archived with absolute path will be put back in place with the original permissions and ownership as sudo will escalate the privileges to the needed level while -C / will untar the files in the proper place provided that the tar archive has been created with absolute paths and that the target restore needs to be put in the very same place.

Now, as you are using a library, the whole process would need to be run as root in order to be able to transfer the ownership. Which in turn could be a bad idea depending upon the specific constraints and use cases.

If you could instead spawn a process running just the unarchiving task, you would be able to let it alone escalate permissions with sudo. In this very case, again, I would use tar instead of a brand new program.

If your needs are just limited to the file access permissions without any ownership information, you can check this other answer of mine. Keep in mind that plain access permissions could not work properly when not followed by the proper ownership information.

  • See link for more details – MBaev May 18 '18 at 8:43
  • 1
    @MBaev Keep in mind that the concept of "file pemissions" is quite articulate: it's not just the bits. – EnzoR May 18 '18 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.