doumeng06063991 2015-01-31 08:42
浏览 154
已采纳

是否可以在golang中提取tar.xz包?

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.

  • 写回答

3条回答 默认 最新

  • dqq9695 2015-07-18 12:13
    关注

    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()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongzhi9574 2015-01-31 10:07
    关注

    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()
    
    评论
  • duanshang7007 2015-01-31 14:29
    关注

    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
    }
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何利用闲置机械硬盘变现
  • ¥15 信号处理中的凸优化问题
  • ¥15 arm虚拟机无法和物理机互通
  • ¥15 如何在此代码上增加一个统计学生生源的功能?(语言-c语言)
  • ¥15 Android导航条遮盖异常
  • ¥15 计算机网络技术基础问题
  • ¥15 设置mac系统只能访问指定网站
  • ¥15 西门子博途 s7 1200控制三台步进电机
  • ¥15 基于非参数的方向距离函数求污染物影子价格(有偿)
  • ¥15 vue+element 生成table