经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
[全网唯一]通过修改源码使得从ZIP提取文件并在提取时进行重命名保存(博客园同步发布)
来源:cnblogs  作者:挥手捧星落  时间:2023/10/25 21:27:09  对本文有异议
源码位置: /Lib/zipfile.py/ZipFile/_extract_member/zipfile.py或者直接点击extract函数.

在使用python解压缩zip文件时, 由于需要在解压时重命名文件为我想要的格式, 而不巧的是, zipfile包官方源代码没有这个功能...

于是, 在百度之后, 果断放弃寻找现成代码的想法.

在研究了一下extract函数的原源代码后, 觉得可以加一个参数targetname用来指代重命名后的文件名, 而很巧的是, 这个新参数并没有在源代码中使用, 所以加入它没有影响.

Talk is easy, show you code~

代码展示

  1. row 1618
  2. def extract(self, member, path=None, pwd=None,targetname=None):
  3. """targetname : the name extracted rename to targetname
  4. Extract a member from the archive to the current working directory,
  5. using its full name. Its file information is extracted as accurately
  6. as possible. `member' may be a filename or a ZipInfo object. You can
  7. specify a different directory using `path'.
  8. """
  9. if path is None:
  10. path = os.getcwd()
  11. else:
  12. path = os.fspath(path)
  13. return self._extract_member(member, path, pwd,targetname)
  14. def extractall(self, path=None, members=None, pwd=None,targetname=None):
  15. """Extract all members from the archive to the current working
  16. directory. `path' specifies a different directory to extract to.
  17. `members' is optional and must be a subset of the list returned
  18. by namelist().
  19. """
  20. if members is None:
  21. members = self.namelist()
  22. if path is None:
  23. path = os.getcwd()
  24. else:
  25. path = os.fspath(path)
  26. for zipinfo in members:
  27. self._extract_member(zipinfo, path, pwd,targetname)
  28. row 1650
  29. ...
  30. row 1665
  31. def _extract_member(self, member, targetpath, pwd,targetname):
  32. """Extract the ZipInfo object 'member' to a physical
  33. file on the path targetpath.
  34. """
  35. if not isinstance(member, ZipInfo):
  36. member = self.getinfo(member)
  37. # build the destination pathname, replacing
  38. # forward slashes to platform specific separators.
  39. arcname = member.filename.replace('/', os.path.sep)
  40. if os.path.altsep:
  41. arcname = arcname.replace(os.path.altsep, os.path.sep)
  42. # interpret absolute pathname as relative, remove drive letter or
  43. # UNC path, redundant separators, "." and ".." components.
  44. arcname = os.path.splitdrive(arcname)[1]
  45. invalid_path_parts = ('', os.path.curdir, os.path.pardir)
  46. arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)
  47. if x not in invalid_path_parts)
  48. if os.path.sep == '\\':
  49. # filter illegal characters on Windows
  50. arcname = self._sanitize_windows_name(arcname, os.path.sep)
  51. if targetname is None:
  52. targetpath = os.path.join(targetpath, arcname)
  53. targetpath = os.path.normpath(targetpath)
  54. else:
  55. targetpath = os.path.normpath(targetpath)
  56. # Create all upper directories if necessary.
  57. upperdirs = os.path.dirname(targetpath)
  58. if upperdirs and not os.path.exists(upperdirs):
  59. os.makedirs(upperdirs)
  60. if member.is_dir():
  61. if not os.path.isdir(targetpath):
  62. os.mkdir(targetpath)
  63. return targetpath
  64. if targetname is None:
  65. with self.open(member, pwd=pwd) as source, open(targetpath, "wb") as target:
  66. shutil.copyfileobj(source, target)
  67. else:
  68. with self.open(member, pwd=pwd) as source, open(targetpath+"/"+targetname, "wb") as target:
  69. shutil.copyfileobj(source, target)
  70. return targetpath
  71. row 1713

  用法

可以直接粘贴到自己的源码中, 如果担心出现其他bug, 可以用完就重装zipfile.

调用extract时传入三个参数: 压缩包名称, 目标目录, 目标名称(重命名后的名字, 如果为None则默认原命名)

原文链接:https://www.cnblogs.com/zenolab/p/17788173.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号