Preserving permissions while zipping

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:

  • Is ZIP archive capable of storing permissions?
  • Can i store unix permissions in a zip file (built with apache ant)?

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.


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.

Tags:

Zip