Last active
March 26, 2020 22:16
-
-
Save jsmolina/1e44073ecbe16bb05a9b7bafe0c19c2a to your computer and use it in GitHub Desktop.
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
""" | |
Converts in batch 7zip files to zip | |
""" | |
import zipfile | |
import glob | |
from io import BytesIO | |
import argparse | |
# Uncomment to use macos hack brew version of of libarchive | |
# import os | |
# os.environ['LA_LIBRARY_FILEPATH']='/usr/local/opt/libarchive/lib/libarchive.dylib' | |
import libarchive.public | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-i','--input', help='input directory glob (e.g. /path/to/*.7z)"', required=True) | |
parser.add_argument('-o','--output', help='output directory', required=True) | |
args = parser.parse_args() | |
files = glob.glob(args.input) | |
for fname in files: | |
zippyname = fname.split('/')[-1].split('.7z')[0] | |
with open(fname, 'rb') as f: | |
buffer_ = f.read() | |
newzip = '{0}/{1}.zip'.format(args.output, zippyname) | |
print("{0} write".format(newzip)) | |
with zipfile.ZipFile(file=newzip, mode='a', compression=zipfile.ZIP_DEFLATED) as zipf: | |
with libarchive.public.memory_reader(buffer_) as e: | |
for entry in e: | |
fio = BytesIO() | |
for block in entry.get_blocks(): | |
fio.write(block) | |
fio.flush() | |
fio.seek(0) | |
zipf.writestr(str(entry.pathname), fio.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment