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

Dha*_*ran 6 compression go lz4

我想在golang中使用lz4算法压缩和解压缩文件.有没有可用的包呢?我搜索并找到了一个名为https://github.com/pierrec/lz4的软件包

我是新的Golang,我无法弄清楚如何使用这个包来压缩和解压缩文件.

我需要将此包用于__CODE__文件__CODE__并使用Golang 解压缩__CODE____CODE__.

谢谢

May*_*tel 6

我认为打击的例子应该引导你纠正方向.这是如何使用github.com/pierrec/lz4包进行压缩和解压缩的最简单示例.

//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("\ndecompressed Data:", string(decompressed[:l]))
}
Run Code Online (Sandbox Code Playgroud)