首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何zstd压缩输入文件中的字节块?

如何zstd压缩输入文件中的字节块?
EN

Stack Overflow用户
提问于 2018-01-11 16:45:37
回答 1查看 813关注 0票数 1

没有足够的zstd压缩示例。我使用的是zstandard 0.8.1,试图一次压缩2字节。在使用write_to(fh)时遇到了https://anaconda.org/rolando/zstandard,但不确定如何使用它。下面是我的部分代码,试图从文件中读取chuck字节,然后将每个chuck,cctx = zstd.ZstdCompressor(level=4) with open(path,'rb')压缩为fh: while True: bin_data = fh.read(2) #read 2 bytes if not bin_data: break压缩= cctx.compress(bin_data) fh.close()

代码语言:javascript
复制
with open(path, 'rb') as fh:
    with open(outpath, 'wb') as outfile:
        outfile.write(compressed)
        ...

但是我该如何使用write_to()呢?

EN

回答 1

Stack Overflow用户

发布于 2018-01-12 18:05:41

我想我找到了正确的方法来使用zstd 0.8.1模块来流式传输字节块:

代码语言:javascript
复制
with open(filename, 'wb') as fh:
    cctx = zstd.ZstdCompressor(level=4)

    with cctx.write_to(fh) as compressor:
        compressor.write(b'data1')
        compressor.write(b'data2')        

with open(filename, 'rb') as fh:
    cctx = zstd.ZstdCompressor(level=4)

    for chunk in cctx.read_from(fh, read_size=128, write_size=128):
        #do something
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48202879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档