Read xz files in go

前提是你 提交于 2019-12-05 03:41:10

问题


How can I read xz files in a go program? When I try to read them using lzma, I get an error in lzma header error.


回答1:


You have 3 options.

  1. Try another library, perhaps one that uses cgo. I see two here.
  2. Use cgo directly/make your own lib.
  3. Use the xz executable.

Option three is easier than it sounds. Here is what I would use:

func xzReader(r io.Reader) io.ReadCloser {
    rpipe, wpipe := io.Pipe()

    cmd := exec.Command("xz", "--decompress", "--stdout")
    cmd.Stdin = r
    cmd.Stdout = wpipe

    go func() {
        err := cmd.Run()
        wpipe.CloseWithError(err)
    }()

    return rpipe
}

Runnable code here: http://play.golang.org/p/SrgZiKdv9a




回答2:


I recently created an XZ decompression package. It does not require Cgo. You can find it here:

https://github.com/xi2/xz

A program to decompress stdin to stdout:

package main

import (
    "io"
    "log"
    "os"

    "github.com/xi2/xz"
)

func main() {
    r, err := xz.NewReader(os.Stdin, 0)
    if err != nil {
        log.Fatal(err)
    }
    _, err = io.Copy(os.Stdout, r)
    if err != nil {
        log.Fatal(err)
    }
}


来源:https://stackoverflow.com/questions/19555373/read-xz-files-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!