猿问

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

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

我是 Go 新手,我不知道如何使用这个包来压缩和解压缩文件。

我需要使用此包将文件压缩为二进制格式,并使用 Go 将二进制文件解压缩为原始文件。


莫回无
浏览 1885回答 3
3回答

牧羊人nacy

我认为打击例子应该引导你走向正确的方向。它是如何使用github.com/pierrec/lz4包压缩和解压缩的最简单示例。//compress project main.gopackage mainimport "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]))}

慕森卡

使用 bufio 包,您可以(解)压缩文件,而无需一次性将文件的全部内容全部放入您的内存中。实际上,这允许您(解)压缩大于系统可用内存的文件,这可能与您的特定情况相关,也可能不相关。如果这是相关的,您可以在此处找到一个工作示例:package mainimport (    "bufio"    "io"    "os"    "github.com/pierrec/lz4")// Compress a file, then decompress it again!func main() {    compress("./compress-me.txt", "./compressed.txt")    decompress("./compressed.txt", "./decompressed.txt")}func compress(inputFile, outputFile string) {    // open input file    fin, err := os.Open(inputFile)    if err != nil {        panic(err)    }    defer func() {        if err := fin.Close(); err != nil {            panic(err)        }    }()    // make a read buffer    r := bufio.NewReader(fin)    // open output file    fout, err := os.Create(outputFile)    if err != nil {        panic(err)    }    defer func() {        if err := fout.Close(); err != nil {            panic(err)        }    }()    // make an lz4 write buffer    w := lz4.NewWriter(fout)    // make a buffer to keep chunks that are read    buf := make([]byte, 1024)    for {        // read a chunk        n, err := r.Read(buf)        if err != nil && err != io.EOF {            panic(err)        }        if n == 0 {            break        }        // write a chunk        if _, err := w.Write(buf[:n]); err != nil {            panic(err)        }    }    if err = w.Flush(); err != nil {        panic(err)    }}func decompress(inputFile, outputFile string) {    // open input file    fin, err := os.Open(inputFile)    if err != nil {        panic(err)    }    defer func() {        if err := fin.Close(); err != nil {            panic(err)        }    }()    // make an lz4 read buffer    r := lz4.NewReader(fin)    // open output file    fout, err := os.Create(outputFile)    if err != nil {        panic(err)    }    defer func() {        if err := fout.Close(); err != nil {            panic(err)        }    }()    // make a write buffer    w := bufio.NewWriter(fout)    // make a buffer to keep chunks that are read    buf := make([]byte, 1024)    for {        // read a chunk        n, err := r.Read(buf)        if err != nil && err != io.EOF {            panic(err)        }        if n == 0 {            break        }        // write a chunk        if _, err := w.Write(buf[:n]); err != nil {            panic(err)        }    }    if err = w.Flush(); err != nil {        panic(err)    }}

Cats萌萌

结果我所期望的是来自下面的代码。我得到了这个 [ https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fpierrec%2Flz4%2Fblob%2Fmaster%2Flz4c%2Fmain.go&sa=D&sntz=1&usg=AFQjCNFIT2O1Grs0vu4Gh8Af9 ]文件。文件作为命令行参数中的输入给出,并且其压缩/解压缩成功。package mainimport (&nbsp; &nbsp; //&nbsp; "bytes"&nbsp; &nbsp; "flag"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io"&nbsp; &nbsp; "log"&nbsp; &nbsp; "os"&nbsp; &nbsp; "path"&nbsp; &nbsp; "runtime"&nbsp; &nbsp; "strings"&nbsp; &nbsp; "github.com/pierrec/lz4")func main() {&nbsp; &nbsp; // Process command line arguments&nbsp; &nbsp; var (&nbsp; &nbsp; &nbsp; &nbsp; blockMaxSizeDefault = 4 << 20&nbsp; &nbsp; &nbsp; &nbsp; flagStdout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = flag.Bool("c", false, "output to stdout")&nbsp; &nbsp; &nbsp; &nbsp; flagDecompress&nbsp; &nbsp; &nbsp; = flag.Bool("d", false, "decompress flag")&nbsp; &nbsp; &nbsp; &nbsp; flagBlockMaxSize&nbsp; &nbsp; = flag.Int("B", blockMaxSizeDefault, "block max size [64Kb,256Kb,1Mb,4Mb]")&nbsp; &nbsp; &nbsp; &nbsp; flagBlockDependency = flag.Bool("BD", false, "enable block dependency")&nbsp; &nbsp; &nbsp; &nbsp; flagBlockChecksum&nbsp; &nbsp;= flag.Bool("BX", false, "enable block checksum")&nbsp; &nbsp; &nbsp; &nbsp; flagStreamChecksum&nbsp; = flag.Bool("Sx", false, "disable stream checksum")&nbsp; &nbsp; &nbsp; &nbsp; flagHighCompression = flag.Bool("9", false, "enabled high compression")&nbsp; &nbsp; )&nbsp; &nbsp; flag.Usage = func() {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(os.Stderr, "Usage:\n\t%s [arg] [input]...\n\tNo input means [de]compress stdin to stdout\n\n", os.Args[0])&nbsp; &nbsp; &nbsp; &nbsp; flag.PrintDefaults()&nbsp; &nbsp; }&nbsp; &nbsp; flag.Parse()&nbsp; &nbsp; fmt.Println("output to stdout ", *flagStdout)&nbsp; &nbsp; fmt.Println("Decompress", *flagDecompress)&nbsp; &nbsp; // Use all CPUs&nbsp; &nbsp; runtime.GOMAXPROCS(runtime.NumCPU())&nbsp; &nbsp; zr := lz4.NewReader(nil)&nbsp; &nbsp; zw := lz4.NewWriter(nil)&nbsp; &nbsp; zh := lz4.Header{&nbsp; &nbsp; &nbsp; &nbsp; BlockDependency: *flagBlockDependency,&nbsp; &nbsp; &nbsp; &nbsp; BlockChecksum:&nbsp; &nbsp;*flagBlockChecksum,&nbsp; &nbsp; &nbsp; &nbsp; BlockMaxSize:&nbsp; &nbsp; *flagBlockMaxSize,&nbsp; &nbsp; &nbsp; &nbsp; NoChecksum:&nbsp; &nbsp; &nbsp; *flagStreamChecksum,&nbsp; &nbsp; &nbsp; &nbsp; HighCompression: *flagHighCompression,&nbsp; &nbsp; }&nbsp; &nbsp; worker := func(in io.Reader, out io.Writer) {&nbsp; &nbsp; &nbsp; &nbsp; if *flagDecompress {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("\n Decompressing the data")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zr.Reset(in)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if _, err := io.Copy(out, zr); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatalf("Error while decompressing input: %v", err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zw.Reset(out)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zw.Header = zh&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if _, err := io.Copy(zw, in); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatalf("Error while compressing input: %v", err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // No input means [de]compress stdin to stdout&nbsp; &nbsp; if len(flag.Args()) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; worker(os.Stdin, os.Stdout)&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(0)&nbsp; &nbsp; }&nbsp; &nbsp; // Compress or decompress all input files&nbsp; &nbsp; for _, inputFileName := range flag.Args() {&nbsp; &nbsp; &nbsp; &nbsp; outputFileName := path.Clean(inputFileName)&nbsp; &nbsp; &nbsp; &nbsp; if !*flagStdout {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if *flagDecompress {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputFileName = strings.TrimSuffix(outputFileName, lz4.Extension)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if outputFileName == inputFileName {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatalf("Invalid output file name: same as input: %s", inputFileName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputFileName += lz4.Extension&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; inputFile, err := os.Open(inputFileName)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatalf("Error while opening input: %v", err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; outputFile := os.Stdout&nbsp; &nbsp; &nbsp; &nbsp; if !*flagStdout {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputFile, err = os.Create(outputFileName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatalf("Error while opening output: %v", err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; worker(inputFile, outputFile)&nbsp; &nbsp; &nbsp; &nbsp; inputFile.Close()&nbsp; &nbsp; &nbsp; &nbsp; if !*flagStdout {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputFile.Close()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}样本输入去运行 compress.go -9=true sample.txt
随时随地看视频慕课网APP

相关分类

Go
我要回答