Created
April 26, 2023 12:09
-
-
Save anadius/83b411c906f3bdc5446534ed207b7173 to your computer and use it in GitHub Desktop.
Extract ZIP when you're low on space
This file contains hidden or 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
""" | |
Quick and dirty script to extract ZIP files when you don't have enough space | |
for both ZIP and extracted files at the same time. | |
It extracts files from the end of the ZIP and then removes them from the archive. | |
Even if you abort with Ctrl+C the ZIP file should be valid. | |
First parameter is the ZIP file. | |
Second parameter is the output dir. | |
""" | |
import sys | |
import zipfile | |
out = sys.argv[2] | |
with zipfile.ZipFile(sys.argv[1], "a") as zf: | |
try: | |
zf._didModify = True | |
zf.filelist.sort(key=lambda x: x.header_offset) | |
while len(zf.filelist) > 0: | |
zinfo = zf.filelist.pop() | |
print(zinfo.filename) | |
zf.extract(zinfo, path=out) | |
try: | |
zf.start_dir = zinfo.header_offset | |
zf.fp.seek(zinfo.header_offset) | |
zf.fp.truncate() | |
except KeyboardInterrupt: | |
zf.start_dir = zinfo.header_offset | |
zf.fp.seek(zinfo.header_offset) | |
zf.fp.truncate() | |
break | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment