Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I preserve symlinks when unzipping an archive using Python?

Tags:

python

Many zip archives (especially those include OS X applications) contain symlinks. When using the zipfile.extractall method, symlinks are turned into regular files. Anyone know how to preserve them as links?

like image 527
derferman Avatar asked Nov 02 '13 00:11

derferman


2 Answers

There seems to be no way to do this using the zipfile module. I solved it using the subprocess module:

from subprocess import check_output, CalledProcessError, STDOUT

try:
   check_output(['unzip', '-q', my_zipfile, '-d', destination], stderr=STDOUT)

...

except CalledProcessError as err:
   (use err.cmd, err.returncode and err.output to take action)
like image 53
Steve Broberg Avatar answered Nov 10 '22 07:11

Steve Broberg


Not using the extractall method. You'll need to do it manually, probably ending with something looking something like this (except you're extracting not compressing).

like image 2
VooDooNOFX Avatar answered Nov 10 '22 07:11

VooDooNOFX