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.
时间: 2024-10-07 14:31:23