def main(): # Read arguments argument_parser = build_argument_parser() args = argument_parser.parse_args() chapter_dir = args.chapter_dir chapter_metadata_filename = 'eduhoribe.json' if not path.isdir(chapter_dir): fatal('Directory "{}" does not exists'.format(chapter_dir)) sys.exit(1) zip_files = [ join(chapter_dir, file) for file in listdir(chapter_dir) if file.endswith('.zip') ] metadata_chapters = { file: file.rstrip('.zip') + '.json' for file in zip_files if isfile(file.rstrip('.zip') + '.json') } for chapter, metadata in metadata_chapters.items(): with zipfile.ZipFile(chapter, 'r') as zip_file: contains_metadata = len([ file for file in zip_file.namelist() if file == chapter_metadata_filename ]) > 0 if contains_metadata: zip_utils.delete_from_zip_file( chapter, file_names=chapter_metadata_filename) with zipfile.ZipFile(chapter, 'a') as zip_file: zip_file.write(metadata, chapter_metadata_filename)
def remove_link(path): """ Take the path of a docx file as argument, return the text in unicode. """ document = zipfile.ZipFile(path) print(document.namelist()) xml_content = document.read('word/document.xml') document.close() xml_content = xml_content.decode('utf-8') xml_content = xml_content.replace("</w:hyperlink>", "") xml_content = re.sub('<w:hyperlink[^>]*>', "", xml_content) # print(xml_content) # xml_content = xml_content.encode('utf-8') tmpfile = open("../document.xml", "w") tmpfile.write(xml_content) # document.writestr("/document.xml", xml_content) zip.delete_from_zip_file(path, pattern=None, file_names='word/document.xml') with zipfile.ZipFile(path, 'a') as myzip: myzip.write('document.xml', "/word/document.xml") myzip.close() # document.write("document.xml") # if __name__ == '__main__': # remove_link("test1.zip")
def fill_metadata(settings, comic, chapters, assembled_ebooks): publishers, languages = chapter_publishers_and_languages(chapters) for volume, ebook_file in assembled_ebooks.items(): cloud_cover_url = None # try to get cover from list if volume in comic.volume_covers: cloud_cover_url = comic.volume_covers[volume] # or use comic generic cover, if exists elif '' in comic.volume_covers: cloud_cover_url = comic.volume_covers[''] if cloud_cover_url is None: warning('Volume "{}" not found in the list of covers...'.format( volume)) continue response = requests.get(cloud_cover_url) local_cover_path = join(settings.temp_directory(comic), volume_pattern(comic.title, volume), 'cover') with open(local_cover_path, 'wb') as local_cover: local_cover.write(response.content) zip_cover_path = join('OEBPS', 'Images', 'cover.jpg') zip_utils.delete_from_zip_file(ebook_file, file_names=zip_cover_path) with zipfile.ZipFile(ebook_file, 'a') as ebook: ebook.write(local_cover_path, zip_cover_path)
def repairaia(aia_path='../sample.aia'): """ Repair .aia. - Fixes "project in project" error. """ will_deleted = list() print("Repairing project...") zip_file = zipfile.ZipFile(aia_path, 'r') # Checks the "project in project" situation and adds the all unwanted file # names to a list. # ...for example Screen files in the assets folder are unexpected. for file_name in zip_file.namelist(): if file_name.startswith( ("assets/external_comps/assets/external_comps/", "assets/external_comps/src/", "assets/external_comps/youngandroidproject/")): will_deleted.append(file_name) print("[-] - DELETED: " + file_name) # It is not needed to store .aia files as assets too. elif file_name.endswith(".aia"): will_deleted.append(file_name) print("[-] - DELETED: " + file_name) # Delete unwanted files from the .aia file. delete_from_zip_file(aia_path, file_names=will_deleted) # If will_deleted list is empty, this means there is nothing to edit. if will_deleted == list(): print("Nothing found to repair.") print("") print("[#] Repair finished. Affected files: " + str(len(will_deleted))) zip_file.close() return True
def delete_zip(file_name): ruamzip.delete_from_zip_file('password/password.zip', file_names=[file_name])
import ruamel.std.zipfile as zipraum import os import zipfile as zipper import shutil directory = 'zips' filetype = 'jpg' destination = 'readUKIndex' for file in os.listdir(directory): if file.endswith(".zip"): #Clean up jpgs from zip zipraum.delete_from_zip_file(directory +'/'+ file, pattern='.*.'+filetype) #Read first file from zip myzip = zipper.ZipFile(directory +'/'+ file,'r') firstFile = myzip.namelist()[0] myzip.close() # check if renaming is necessary if (file[:10]==firstFile[:10]): print ("OK") else: print ("rename\n", firstFile[:10]+"-"+file[11:]) #Rename zip file according to the date of the first XML os.rename(directory +'/'+ file,directory +'/'+firstFile[:10]+"-"+file[11:])
def delete_file_from_zip(newzip, file): """Delete a file in a archive. You need to have a zipfile object opened with write_zip(). Usage : delete_file_from_zip(z, 'file.txt')""" delete_from_zip_file(newzip, file_names=[str(file)])
def cleanaia(aia_path='../sample.aia'): """ Clean-up the .aia. - Scans the .bky and .scm files, and deletes all assets which is not refere nced in these files. """ will_deleted = list() print("Cleaning the project...") zip_file = zipfile.ZipFile(aia_path, 'r') # Content of all .bky files in project. bky = list() # Content of all .scm files in project. scm = list() # Content of the project.properties file. prop = "" # Reads .bky, .scm and project.properties files from project and saves into # a variable. for file_name in zip_file.namelist(): if file_name.endswith(".bky"): bky.append(zip_file.read(file_name).decode(sys.stdout.encoding)) elif file_name.endswith(".scm"): scm.append(zip_file.read(file_name).decode(sys.stdout.encoding)) elif "project.properties" in file_name: prop = prop + zip_file.read(file_name).decode(sys.stdout.encoding) # Converts list to string for checking the references. filebky = "".join(bky) filescm = "".join(scm) for file_name in zip_file.namelist(): # Gets the name of the file by deleting the path piece. name = file_name.split("/")[-1] if file_name.startswith("assets/") and \ "external_comps/" not in file_name: # Checks if the name is already used in project files. if name in filebky: pass elif name in filescm: pass elif name in prop: pass else: will_deleted.append(file_name) print("DELETED: " + file_name) # Delete unwanted files from the .aia file. delete_from_zip_file(aia_path, file_names=will_deleted) # If will_deleted list is empty, this means there is nothing to edit. if will_deleted == list(): print("Nothing found to cleanup.") print("") print("[#] Cleanup finished. Affected files: " + str(len(will_deleted))) zip_file.close() return True