When using Apache Commons Compress to extract a tar file, how do I find out the file permissions (read, write, executable) of each TarArchiveEntry?
By default, tar will preserve file permissions and ownership when creating the archive. To extract file permissions and ownership, you will need to run tar as root when extracting, since changing file ownership usually requires superuser privileges.
Check Permissions in Command-Line with Ls Command If you prefer using the command line, you can easily find a file's permission settings with the ls command, used to list information about files/directories. You can also add the –l option to the command to see the information in the long list format.
TarArchiveEntry provides a method "getMode()" which returns the Unix file mode, e.g.
TarArchiveEntry entry = input.getNextTarEntry();
while(entry != null) {
System.out.println("Entry: " + entry.getName() + ", Mode: " + entry.getMode());
entry = input.getNextTarEntry();
}
with a test-tar-file it will result in:
Entry: usr/local/bin/bcdiff, Mode: 493
Entry: usr/local/bin/jgrep, Mode: 493
Entry: usr/local/bin/ysh, Mode: 365
which translates to:
-rwxr-xr-x bcdiff
-rwxr-xr-x jgrep
-r-xr-xr-x ysh
You can read up on details about the mode numbers on many sites in the Internet, e.g. here
HTH... Dominik.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With