Skip to content

Instantly share code, notes, and snippets.

@ayyybe
Last active July 21, 2022 19:33
Show Gist options
  • Save ayyybe/b67f67d872bb38afc8333f25ecb4ba9f to your computer and use it in GitHub Desktop.
Save ayyybe/b67f67d872bb38afc8333f25ecb4ba9f to your computer and use it in GitHub Desktop.
extract zip keeping permissions & symlinks intact
import zipfile
import os
import stat
def extract_file(zf, info, extract_dir):
out_path = os.path.join(extract_dir, info.filename)
perm = info.external_attr >> 16
if (stat.S_ISLNK(perm)):
src = zf.open(info).read()
dst = out_path
os.symlink(src, dst)
else:
zf.extract(info, extract_dir)
os.chmod(out_path, perm)
with zipfile.ZipFile('ye.zip', 'r') as zf:
for info in zf.infolist():
extract_file(zf, info, 'out')
import zipfile
import os
import stat
def extract_file(zf, info, extract_dir):
out_path = os.path.join(extract_dir, info.filename)
perm = info.external_attr >> 16
zf.extract(info, extract_dir)
os.chmod(out_path, perm)
with zipfile.ZipFile('ye.zip', 'r') as zf:
for info in zf.infolist():
extract_file(zf, info, 'out')
@duolabmeng6
Copy link

Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment