首页 > 代码库 > 【Python】zlib压缩文件

【Python】zlib压缩文件

import zlib
import os

ss = ‘s‘ * 1024 * 1024

#写入原始文件
file = open("src.dat", "wb")
file.write(ss.encode())
file.close()

#读取上一步原始的文件
file = open("src.dat", "rb")
sss = file.read(os.path.getsize("src.dat"))     #获取文件大小
file.close()

#压缩文件
compress = zlib.compress(sss)

#写入压缩文件
file = open("compress.dat", "wb")
file.write(compress)
file.close()

#读取上一步的压缩文件
file = open("compress.dat", ‘rb‘)
sss = file.read(os.path.getsize("compress.dat"))
file.close()

decompress = zlib.decompress(sss)

#写入还原文件
file = open("decompress.dat", "wb")
file.write(decompress)
file.close()

print(len(ss))
print(len(compress))
print(len(decompress))

  

【Python】zlib压缩文件