Python2 中,如何使用 ZipFile 在 APK 文件末尾续写文件?
为了在末尾续写文件而不是删除并重新创建文件,你需要将"ZipFile"的打开模式从"w"改为"a"。以下是修改后的代码:\n\npython\ndef apk_add_file(file_path, apk_file_path, apk_file_name, vivii):\n try:\n if public_function.is_file_exist(file_path) and public_function.is_file_exist(apk_file_path):\n zf = zipfile.ZipFile(apk_file_path, \"a\", zipfile.ZIP_DEFLATED)\n name_list = zf.namelist()\n if apk_file_name in name_list:\n zf.close()\n apk_delete_file(apk_file_path, apk_file_name)\n zf = zipfile.ZipFile(apk_file_path, \"a\", zipfile.ZIP_DEFLATED)\n zf.write(file_path, apk_file_name)\n print(\"if apk_file_name in name_list:\" + vivii)\n zf.close()\n else:\n print(\"else:\" + vivii)\n zf.write(file_path, apk_file_name)\n zf.close()\n return True\n else:\n logging.error(\"the \" + file_path + \" or \" + apk_file_path + \" does not exist!\")\n return False\n except IOError:\n return False\n\n\n在这个修改后的代码中,"ZipFile"的打开模式被改为"a",这表示以添加模式打开文件,以便在末尾续写数据。
原文地址: https://www.cveoy.top/t/topic/pZwa 著作权归作者所有。请勿转载和采集!