import zipfile,os,sys 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)) f = zipfile.ZipFile(zipfilename, "w", zipfile.ZIP_DEFLATED) #ZIP_STORE, zipfile.ZIP_DEFLATED for tar in filelist: arcname = tar[len(dirname):] f.write(tar, arcname) f.close() if len(sys.argv) > 2 and sys.argv[1] != "" and sys.argv[2] != "": zip_dir(sys.argv[1], sys.argv[2]) else: print("#error params")
使用方法如下:
compress.py [dir] [zipfile]
或
python compress.py [dir] [zipfile]