python自带zipfile模块用来读、写zip文件。

 

def zip_dir(dirname,zipfilename):
    filelist = []
    if os.path.isfile(dirname):
        filelist.append(dirname)
    else :
        for root, dirs, files in os.walk(dirname):
            for name in files:
                filelist.append(os.path.join(root, name))
         
    zf = zipfile.ZipFile(zipfilename, "w", zipfile.ZIP_STORED,allowZip64=True)
    for tar in filelist:
        arcname = tar[len(dirname):]
        #print arcname
        zf.write(tar,arcname)
    zf.close() 

  

zf = zipfile.ZipFile(zipfilename, "w", zipfile.ZIP_STORED,allowZip64=True)
上面的函数有四个函数:zip文件包含路径的名字,"w"/"r"表示写或者读,zipfile.ZIP_STORED表示存储格式(还可以是zipfile.zlib.DEFLATED表示压缩格式),allowZip64=True这个参数是在处理大文件时使用的,默认为False。如果没有设置为True而处理大文件时会提示 zip file size require ZIP64 extensions.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-18
  • 2021-11-24
  • 2021-11-14
  • 2021-12-28
  • 2022-01-16
  • 2022-12-23
猜你喜欢
  • 2021-11-21
  • 2021-06-24
  • 2022-01-14
  • 2021-07-02
  • 2022-12-23
  • 2021-09-19
相关资源
相似解决方案