26

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?

3 Answers 3

20

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.

On other systems, the ability to save/restore permissions is less complete. For example, on Windows the ZIP file uses the permission settings from the %temp% folder.

Further reading:

4
  • Excellent answer! I did not know that about info-zip.
    – anon
    Oct 2, 2016 at 8:35
  • I tried to download infozip, but win10 report it is virus contained.
    – netawater
    Aug 25, 2019 at 6:34
  • You could get the binaries from various places (not all good). It's been widely redistributed. Aug 25, 2019 at 10:31
  • 1
    But how do I set the execute file mode for a .sh file on Windows so when its extracted on Linux it works?
    – Petah
    Oct 21, 2020 at 14:05
20

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.

5

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.

2
  • 2
    See superuser.com/a/940477/109788 . I can view permissions with zip -Z <file> .
    – Binoy Babu
    Oct 1, 2016 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.
    – anon
    Oct 1, 2016 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .