欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > cryptography与zlib系列:数据压缩与解压

cryptography与zlib系列:数据压缩与解压

2025/4/18 21:47:41 来源:https://blog.csdn.net/familytaijun/article/details/143982152  浏览:    关键词:cryptography与zlib系列:数据压缩与解压

cryptography与zlib系列:数据压缩与解压

压缩

import zlibdef get_zlib_compressed_data(file_path: str,compress_level: int = 9,
) -> bytes:with open(file_path, 'rb') as f:data = f.read()compressed_data = zlib.compress(data, compress_level)return compressed_dataif __name__ == '__main__':from time import timefile_path = "test.txt"file_compressed_path = "test_compressed.zlib"tic = time()compressed_data = get_zlib_compressed_data(file_path)toc = time()print(f"compress time: {toc - tic}")with open(file_compressed_path, 'wb') as f:f.write(compressed_data)

解压

import zlibif __name__ == '__main__':file_compressed_path = "test_compressed.zlib"file_decompressed_path = "test_decompressed.txt# 读取压缩文件with open(compressed_file_path, 'rb') as fr:compressed_data = fr.read()with open(decompressed_file_path, 'wb') as fw:decompressed_data = zlib.decompress(compressed_data)fw.write(decompressed_data)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词