Skip to content

压缩打包

python
import zipfile
import os

class ZipHelper:
    def __init__(self):
        self.file_count = 0

    def zip_dir(self,dirname,zipfilename):
        filecount = 0
        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.zlib.DEFLATED)
        for tar in filelist:
            arcname = tar[len(dirname):]
            print ("package >>",arcname)
            zf.write(tar,arcname)
            self.file_count += 1
        zf.close()


# zip_helper = ZipHelper()
# zip_helper.zip_dir("./","test.zip")
# print(os.path.getsize("test.zip"))