Skip to content

Instantly share code, notes, and snippets.

@n1ckfg
Last active October 7, 2018 17:58
Show Gist options
  • Save n1ckfg/b27c91707bcf679f884340869ff38a83 to your computer and use it in GitHub Desktop.
Save n1ckfg/b27c91707bcf679f884340869ff38a83 to your computer and use it in GitHub Desktop.
import glob
import os
import zipfile
import tempfile
# https://stackoverflow.com/questions/25738523/how-to-update-one-file-inside-zip-file-using-python
def updateZip(zipname, filename, newFile):
# generate a temp file
tmpfd, tmpname = tempfile.mkstemp(dir=os.path.dirname(zipname))
os.close(tmpfd)
# create a temp copy of the archive without filename
with zipfile.ZipFile(zipname, 'r') as zin:
with zipfile.ZipFile(tmpname, 'w') as zout:
zout.comment = zin.comment # preserve the comment
for item in zin.infolist():
if item.filename != filename:
zout.writestr(item, zin.read(item.filename))
# replace with the temp archive
os.remove(zipname)
os.rename(tmpname, zipname)
# now add filename with its new data
with zipfile.ZipFile(zipname, mode='a', compression=zipfile.ZIP_DEFLATED) as zf:
zf.write(newFile)
def updateCategories(rootUrl, licenseUrl):
categories = glob.glob(rootUrl)
for category in categories:
zipFiles = glob.glob(category + "/*.zip")
for zipFile in zipFiles:
updateZip(zipFile, "rkhive.txt", licenseUrl)
licenseFile = "./rkhive.txt"
updateCategories("./new/*", licenseFile)
updateCategories("./rk-download/*", licenseFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment