【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))

 

posted @ 2018-03-04 11:36  Dsp Tian  阅读(533)  评论(0编辑  收藏  举报