doumei1908 2016-01-25 11:31
浏览 4094
已采纳

如何使用lz4压缩和解压缩文件?

I want to compress and decompress a file using lz4 algorithm in Go. Is there any package available to do this? I searched and found a package called https://github.com/pierrec/lz4

I am new Go and I cannot figure out how to use this package to compress and decompress a file.

I need to use this package to compress a file to binary format and decompress the binary file to original file using Go.

  • 写回答

4条回答 默认 最新

  • douji6735 2016-01-25 13:11
    关注

    I think blow example should direct you to correct direction. It is the simplest example of how to compress and decompress using github.com/pierrec/lz4 package.

    //compress project main.go
    package main
    
    import "fmt"
    import "github.com/pierrec/lz4"
    
    var fileContent = `CompressBlock compresses the source buffer starting at soffet into the destination one.
    This is the fast version of LZ4 compression and also the default one.
    The size of the compressed data is returned. If it is 0 and no error, then the data is incompressible.
    An error is returned if the destination buffer is too small.`
    
    func main() {
        toCompress := []byte(fileContent)
        compressed := make([]byte, len(toCompress))
    
        //compress
        l, err := lz4.CompressBlock(toCompress, compressed, 0)
        if err != nil {
            panic(err)
        }
        fmt.Println("compressed Data:", string(compressed[:l]))
    
        //decompress
        decompressed := make([]byte, len(toCompress))
        l, err = lz4.UncompressBlock(compressed[:l], decompressed, 0)
        if err != nil {
            panic(err)
        }
        fmt.Println("
    decompressed Data:", string(decompressed[:l]))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 自定义 thinkphp 命令行,一直报错,如何解决?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题