Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect file permissions when extracting tar file?

When using Apache Commons Compress to extract a tar file, how do I find out the file permissions (read, write, executable) of each TarArchiveEntry?

like image 716
Gili Avatar asked Feb 24 '12 18:02

Gili


People also ask

Does tar keep file permissions?

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.

How do I check permissions on a file?

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.


1 Answers

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.

like image 125
centic Avatar answered Oct 12 '22 19:10

centic