Skip to content

Instantly share code, notes, and snippets.

@bifocalpirate
Last active June 15, 2021 12:09
Show Gist options
  • Save bifocalpirate/f8eeb6edfc1b156fce1a18b03bbf59bf to your computer and use it in GitHub Desktop.
Save bifocalpirate/f8eeb6edfc1b156fce1a18b03bbf59bf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
This code will process a folder containing .7z archives and output .zip files to a destation folder. It does the .7z extraction
and zipping natively. You can enable file logging at line 24. There are about 15000? files I needed to
convert so that's a useful config item. In my use-case I can assume the source folder is composed entirely of .7z files but I
do catch exceptions appropriately.
This software is released AS-IS. If your kittens end up having nightmares because of it, or something more or less as dramatic
you have been warned.
pip install py7zr progressbar2
"""
import zipfile
import py7zr
import zipfile
from os import listdir, walk, makedirs
import progressbar
from os.path import isfile, join, splitext, basename, exists, sep as pathsep
import shutil
import tempfile
import sys
import logging
sourcefolder = r'./sourcefiles/'
sourcefolder = r'/media/inputfiles/'
outputfolder = r'/media/roms/'
tempfolder = join(tempfile.gettempdir(), "convertorfiles")
tempfolderdepth = len(tempfolder.split(pathsep)) + 1 #archive name is a subfolder
file_logging_enabled = False
if file_logging_enabled:
logging.basicConfig(filename='conversion.log', filemode='w', level = logging.INFO)
if not exists(sourcefolder):
raise 'SourceFolder does not exist.'
if not exists(outputfolder):
makedirs(outputfolder)
print(f"SOURCE FOLDER IS: {sourcefolder}")
print(f"OUTPUT FOLDER IS: {outputfolder}")
print(f"TEMP FOLDER IS: {tempfolder}")
files = [f for f in listdir(sourcefolder) if isfile(join(sourcefolder, f))]
def zipdir(dir_to_archive, archive_filename):
ziph = zipfile.ZipFile(archive_filename, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in walk(dir_to_archive, topdown=True):
for file in files:
if file != archive_filename:
fn = pathsep.join(join(root,file).split(pathsep)[tempfolderdepth:])
ziph.write(join(root, file), arcname=fn)
ziph.close()
def process_archive(sourcefile, tempfolder):
try:
logging.debug(f"Processing {sourcefile}")
archive = py7zr.SevenZipFile(join(sourcefile), mode='r')
archive.extractall(path=tempfolder)
logging.debug(f"\t * Found {len([f for f in listdir(tempfolder) if isfile(join(tempfolder, f))])} files")
outputfile = splitext(basename(sourcefile))[0] + ".zip"
logging.debug(f"\t * Rezipping folder {tempfolder} to {join(outputfolder,outputfile)}")
zipdir(tempfolder, join(outputfolder, outputfile))
logging.debug(f"\t * Removing tempfolder {wf}")
except KeyboardInterrupt:
sys.exit()
except Exception as e:
logging.error(f"{e} ({sourcefile})")
finally:
if exists(tempfolder):
shutil.rmtree(tempfolder)
logging.debug(f"\t * Deleted/Took care of folder {tempfolder}")
for f in progressbar.progressbar(files, redirect_stdout=True):
wf = join(tempfolder, f) + pathsep
process_archive(join(sourcefolder, f), wf)
print("ALL DONE!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment