源碼位置: /Lib/zipfile.py/ZipFile/_extract_member/zipfile.py或者直接點擊extract函數. 在使用python解壓縮zip文件時, 由於需要在解壓時重命名文件為我想要的格式, 而不巧的是, zipfile包官方源代碼沒有這個功能... 於是, 在 ...
源碼位置: /Lib/zipfile.py/ZipFile/_extract_member/zipfile.py或者直接點擊extract函數.
在使用python解壓縮zip文件時, 由於需要在解壓時重命名文件為我想要的格式, 而不巧的是, zipfile包官方源代碼沒有這個功能...
於是, 在百度之後, 果斷放棄尋找現成代碼的想法.
在研究了一下extract函數的原源代碼後, 覺得可以加一個參數targetname用來指代重命名後的文件名, 而很巧的是, 這個新參數並沒有在源代碼中使用, 所以加入它沒有影響.
Talk is easy, show you code~
代碼展示
row 1618 def extract(self, member, path=None, pwd=None,targetname=None): """targetname : the name extracted rename to targetname Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member' may be a filename or a ZipInfo object. You can specify a different directory using `path'. """ if path is None: path = os.getcwd() else: path = os.fspath(path) return self._extract_member(member, path, pwd,targetname) def extractall(self, path=None, members=None, pwd=None,targetname=None): """Extract all members from the archive to the current working directory. `path' specifies a different directory to extract to. `members' is optional and must be a subset of the list returned by namelist(). """ if members is None: members = self.namelist() if path is None: path = os.getcwd() else: path = os.fspath(path) for zipinfo in members: self._extract_member(zipinfo, path, pwd,targetname) row 1650 ... row 1665 def _extract_member(self, member, targetpath, pwd,targetname): """Extract the ZipInfo object 'member' to a physical file on the path targetpath. """ if not isinstance(member, ZipInfo): member = self.getinfo(member) # build the destination pathname, replacing # forward slashes to platform specific separators. arcname = member.filename.replace('/', os.path.sep) if os.path.altsep: arcname = arcname.replace(os.path.altsep, os.path.sep) # interpret absolute pathname as relative, remove drive letter or # UNC path, redundant separators, "." and ".." components. arcname = os.path.splitdrive(arcname)[1] invalid_path_parts = ('', os.path.curdir, os.path.pardir) arcname = os.path.sep.join(x for x in arcname.split(os.path.sep) if x not in invalid_path_parts) if os.path.sep == '\\': # filter illegal characters on Windows arcname = self._sanitize_windows_name(arcname, os.path.sep) if targetname is None: targetpath = os.path.join(targetpath, arcname) targetpath = os.path.normpath(targetpath) else: targetpath = os.path.normpath(targetpath) # Create all upper directories if necessary. upperdirs = os.path.dirname(targetpath) if upperdirs and not os.path.exists(upperdirs): os.makedirs(upperdirs) if member.is_dir(): if not os.path.isdir(targetpath): os.mkdir(targetpath) return targetpath if targetname is None: with self.open(member, pwd=pwd) as source, \ open(targetpath, "wb") as target: shutil.copyfileobj(source, target) else: with self.open(member, pwd=pwd) as source, \ open(targetpath+"/"+targetname, "wb") as target: shutil.copyfileobj(source, target) return targetpath row 1713
用法
可以直接粘貼到自己的源碼中, 如果擔心出現其他bug, 可以用完就重裝zipfile.
調用extract時傳入三個參數: 壓縮包所在目錄, 目標目錄, 目標名稱(重命名後的名字, 如果為None則預設原命名)