在golang中是否可以解压tar.xz包?

huangapple go评论77阅读模式
英文:

Is it possible to extract a tar.xz package in golang?

问题

在golang中,是否可以解压tar.xz包?我的理解是可以使用tar库,并将其发送到一个xz的go库中。

英文:

Is it possible to extract a tar.xz package in golang? My understanding is it's possible to use the library for tar and sending it to an xz go library.

答案1

得分: 6

我最近创建了一个XZ解压缩包,现在可以只使用Go代码来解压缩tar.xz文件。

以下代码将文件myfile.tar.xz解压缩到当前目录:

package main

import (
    "archive/tar"
    "fmt"
    "io"
    "log"
    "os"

    "github.com/xi2/xz"
)

func main() {
    // 打开文件
    f, err := os.Open("myfile.tar.xz")
    if err != nil {
        log.Fatal(err)
    }
    // 创建一个xz Reader
    r, err := xz.NewReader(f, 0)
    if err != nil {
        log.Fatal(err)
    }
    // 创建一个tar Reader
    tr := tar.NewReader(r)
    // 遍历归档中的文件
    for {
        hdr, err := tr.Next()
        if err == io.EOF {
            // tar归档结束
            break
        }
        if err != nil {
            log.Fatal(err)
        }
        switch hdr.Typeflag {
        case tar.TypeDir:
            // 创建目录
            fmt.Println("创建目录:   " + hdr.Name)
            err = os.MkdirAll(hdr.Name, 0777)
            if err != nil {
                log.Fatal(err)
            }
        case tar.TypeReg, tar.TypeRegA:
            // 写入文件
            fmt.Println("提取文件: " + hdr.Name)
            w, err := os.Create(hdr.Name)
            if err != nil {
                log.Fatal(err)
            }
            _, err = io.Copy(w, tr)
            if err != nil {
                log.Fatal(err)
            }
            w.Close()
        }
    }
    f.Close()
}

希望对你有帮助!

英文:

I recently created an XZ decompression package so it is now
possible to extract a tar.xz using only Go code.

The following code extracts the file myfile.tar.xz to the current
directory:

package main
import (
"archive/tar"
"fmt"
"io"
"log"
"os"
"github.com/xi2/xz"
)
func main() {
// Open a file
f, err := os.Open("myfile.tar.xz")
if err != nil {
log.Fatal(err)
}
// Create an xz Reader
r, err := xz.NewReader(f, 0)
if err != nil {
log.Fatal(err)
}
// Create a tar Reader
tr := tar.NewReader(r)
// Iterate through the files in the archive.
for {
hdr, err := tr.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil {
log.Fatal(err)
}
switch hdr.Typeflag {
case tar.TypeDir:
// create a directory
fmt.Println("creating:   " + hdr.Name)
err = os.MkdirAll(hdr.Name, 0777)
if err != nil {
log.Fatal(err)
}
case tar.TypeReg, tar.TypeRegA:
// write a file
fmt.Println("extracting: " + hdr.Name)
w, err := os.Create(hdr.Name)
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(w, tr)
if err != nil {
log.Fatal(err)
}
w.Close()
}
}
f.Close()
}

答案2

得分: 4

以下是要翻译的内容:

http://golang.org/pkg/archive/tar/#example_

你也可以这样做:

import "os/exec"
cmd := exec.Command("tar", "-x", "/your/archive.tar.xz")
err := cmd.Run()
英文:

http://golang.org/pkg/archive/tar/#example_

also you can do

import "os/exec"
cmd := exec.Command("tar", "-x", "/your/archive.tar.xz")
err := cmd.Run()

答案3

得分: 1

在Go标准库中没有Lempel-Ziv-Markow编码器或解码器。如果你可以假设你的代码运行的平台提供了xz实用程序,你可以使用以下类似的存根函数:

import "os/exec"

// 解压缩xz压缩的数据流r。
func UnxzReader(r io.Reader) (io.ReadCloser, error) {
    unxz := exec.Command("xz", "-d")
    unxz.Stdin = r
    out, err := unxz.StdoutPipe()
    if err != nil {
        return nil, err
    }

    err = unxz.Start()
    if err != nil {
        return nil, err
    }

    // 我们对退出状态不感兴趣,但我们确实应该收集那个僵尸进程
    go unxz.Wait()

    return out, nil
}

以上是给定的代码段的翻译结果。

英文:

There is no Lempel-Ziv-Markow encoder or decoder in the Go standard library. If you are allowed to assume that the platform your code runs on provides the xz utility, you could use stub functions like these:

import "os/exec"
// decompress xz compressed data stream r.
func UnxzReader(r io.Reader) (io.ReadCloser, error) {
unxz := exec.Command("xz", "-d")
unxz.Stdin = r
out, err := unxz.StdoutPipe()
if err != nil {
return nil, err
}
err = unxz.Start()
if err != nil {
return nil, err
}
// we are not interested in the exit status, but we should really collect
// that zombie process
go unxz.Wait()
return out, nil
}

huangapple
  • 本文由 发表于 2015年1月31日 16:42:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/28249782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定