Last active
July 21, 2022 19:33
-
-
Save ayyybe/b67f67d872bb38afc8333f25ecb4ba9f to your computer and use it in GitHub Desktop.
extract zip keeping permissions & symlinks intact
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zipfile | |
import os | |
import stat | |
def extract_file(zf, info, extract_dir): | |
if (stat.S_ISLNK(info.external_attr >> 16)): | |
src = zf.open(info).read() | |
dst = os.path.join(extract_dir, info.filename) | |
os.symlink(src, dst) | |
else: | |
zf.extract(info, extract_dir) | |
with zipfile.ZipFile('ye.zip', 'r') as zf: | |
for info in zf.infolist(): | |
extract_file(zf, info, 'out') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much