5

How can I preserve permissions while compressing a folder using zip?

I know how to preserve symlinks using --symlinks is there a similar option for permissions?

8

info-zip (the program you probably are using) can save/restore permissions for Unix-like systems.

It is mentioned for directories in the manual page:

Dates, times and permissions of stored directories are not restored except under Unix. (On Windows NT and successors, timestamps are now restored.)

File-permissions for read/write/execute are saved/restored. But a quick check shows (zip 3.0) that setuid/setgid permissions are not preserved.

The feature is not optional; zip/unzip simply do this when they are able.

Further reading:

  • Excellent answer! I did not know that about info-zip. – maulinglawns Oct 2 '16 at 8:35
  • I tried to download infozip, but win10 report it is virus contained. – netawater Aug 25 at 6:34
  • You could get the binaries from various places (not all good). It's been widely redistributed. – Thomas Dickey Aug 25 at 10:31
4

Short answer: you can't! zip does not preserve file permissions.

Read more about this here and here.

If you need to preserve permissions, please consider using tar with the --preserve-permissions switch instead.

  • See superuser.com/a/940477/109788 . I can view permissions with zip -Z <file> . – Binoy Babu Oct 1 '16 at 15:59
  • @BinoyBabu Yeah, I read that post. I find it to be somewhat ambigous: ”If you didn't make the ZIP file yourself, your only chance is to set the correct the permissions after unzipping”. The keyword here being after. – maulinglawns Oct 1 '16 at 16:10
1

Info-Zip 3.0 SUPPORTS preserving files/dirs UNIX permissions and UID/GID ownership data. zip stores it by default but you need to use unzip in an special way to restore them:

  • unzip must be used with the -X flag.
  • unzip must run as root to set the files/dirs UID/GID. If you run it as a normal user then the UID will be always the one of the current user and the GID will be restored ONLY IF the current user belongs to that group.

Example:

# zip -v | head -2 | tail -1
This is Zip 3.0 (July 5th 2008), by Info-ZIP.

# unzip -v | head -1
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.

# touch file1
# mkdir dir1
# chmod 000 file1
# chown 1111 dir1
# ls -ld file1 dir1
drwxr-xr-x 2 1111 root 40 mar 28 20:12 dir1
---------- 1 root root  0 mar 28 20:12 file1

# zip files.zip file1 dir1
  adding: file1 (stored 0%)
  adding: dir1/ (stored 0%)

# unzip -X files.zip -d extracted
Archive:  files.zip
 extracting: extracted/file1         
   creating: extracted/dir1/

# ls -l extracted
total 0
drwxr-xr-x 2 1111 root 40 mar 28 20:12 dir1
---------- 1 root root  0 mar 28 20:12 file1

Note: you can also use unzip with the -K flag to also restore SUID/SGID/Sticky bits.

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.