从 Python 中的 zip 文件中删除自动生成的 __MACOSX 文件夹 [英] Remove auto-generated __MACOSX folder from inside a zip file in Python

查看:23
本文介绍了从 Python 中的 zip 文件中删除自动生成的 __MACOSX 文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有客户端通过网络服务器上传的 zip 文件,其中有时包含讨厌的 __MACOSX 目录,里面有一些东西.我怎样才能删除这些?

I have zip files uploaded by clients through a web server that sometimes contain pesky __MACOSX directories inside that gum things up. How can I remove these?

我想过使用 ZipFile,但是 this 回答说这是不可能的,并给出了这个建议:

I thought of using ZipFile, but this answer says that isn't possible and gives this suggestion:

读出存档的其余部分并将其写入一个新的 zip 文件.

Read out the rest of the archive and write it to a new zip file.

如何使用 ZipFile 执行此操作?另一个基于 Python 的替代方案,如 shutil 或类似的东西也可以.

How can I do this with ZipFile? Another Python based alternative like shutil or something similar would also be fine.

推荐答案

以下示例旨在确定 '__MACOSX' 文件是否包含在 zip 文件中.如果这个讨厌的存在,那么会创建一个新的 zip 存档,并且所有不是 __MACOSX 文件的文件都被写入这个新存档.可以扩展此代码以包含 .ds_store 文件.如果您需要删除旧的 zip 文件并将其替换为新的干净 zip 文件,请告诉我.

The examples below are designed to determine if a '__MACOSX' file is contained within a zip file. If this pesky exist then a new zip archive is created and all the files that are not __MACOSX files are written to this new archive. This code can be extended to include .ds_store files. Please let me if you need to delete the old zip file and replace it with the new clean zip file.

希望这些答案能帮助您解决问题.

Hopefully, these answers help you solve your issue.

示例一

from zipfile import ZipFile

original_zip = ZipFile ('original.zip', 'r')
new_zip = ZipFile ('new_archve.zip', 'w')
for item in original_zip.infolist():
   buffer = original_zip.read(item.filename)
   if not str(item.filename).startswith('__MACOSX/'):
     new_zip.writestr(item, buffer)
  new_zip.close()
original_zip.close()

示例二

def check_archive_for_bad_filename(file):
  zip_file = ZipFile(file, 'r')
  for filename in zip_file.namelist():
     print(filename)
     if filename.startswith('__MACOSX/'):
        return True

def remove_bad_filename_from_archive(original_file, temporary_file):
   zip_file = ZipFile(original_file, 'r')
   for item in zip_file.namelist():
      buffer = zip_file.read(item)
      if not item.startswith('__MACOSX/'):
        if not os.path.exists(temporary_file):
           new_zip = ZipFile(temporary_file, 'w')
           new_zip.writestr(item, buffer)
           new_zip.close()
         else:
           append_zip = ZipFile(temporary_file, 'a')
           append_zip.writestr(item, buffer)
           append_zip.close()

    zip_file.close()


archive_filename = 'old.zip'
temp_filename = 'new.zip'

results = check_archive_for_bad_filename(archive_filename)
if results:
   print('Removing MACOSX file from archive.')
   remove_bad_filename_from_archive(archive_filename, temp_filename)
else:
   print('No MACOSX file in archive.')

这篇关于从 Python 中的 zip 文件中删除自动生成的 __MACOSX 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆