阅读xz文件 [英] Read xz files in go

查看:111
本文介绍了阅读xz文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在go程序中阅读 xz 文件?当我尝试使用 lzma ,我在lzma头错误中得到一个错误。

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.

推荐答案

您有3个选项。


  1. 尝试另一个库,也许使用cgo。我在这里看到两个

  2. 直接使用cgo /让你的自己的lib。
  3. 使用xz可执行文件。
  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
}

可运行代码: http://play.golang.org/p/SrgZiKdv9a

这篇关于阅读xz文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆