【问题标题】:How to zstd compress a chunk of bytes from an input file?如何zstd从输入文件中压缩一大块字节?
【发布时间】:2018-01-11 08:45:36
【问题描述】:

没有足够的 zstd 压缩示例。我正在使用 zstandard 0.8.1,尝试一次压缩 2 个字节。在使用write_to(fh) 时遇到https://anaconda.org/rolando/zstandard,但不知道如何使用它。下面是我尝试从文件中读取一个chuck字节的部分代码,然后压缩每个chuck, cctx = zstd.ZstdCompressor(level=4) 使用 open(path, 'rb') 作为 fh: 而真: bin_data = fh.read(2) #读取 2 个字节 如果不是 bin_data: 休息 压缩 = cctx.compress(bin_data) fh.close()

with open(path, 'rb') as fh:
    with open(outpath, 'wb') as outfile:
        outfile.write(compressed)
        ...

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

【问题讨论】:

    标签: compression byte zstandard


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2019-03-27
      • 2021-07-04
      • 2021-10-08
      • 1970-01-01
      • 2023-02-05
      • 2020-01-04
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多