如何使用golang的exec.Cmd命令来处理tar命令?

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

How to work with tar command using golang exec.Cmd

问题

我有一个tar.gz文件,我需要使用golang解压它。
我尝试过像"archive/tar"这样的库,但是它们给了我一个错误:archive/tar: invalid tar header. 现在我的想法是使用exec运行tar命令并解压tarball,但它总是以代码2退出。

我的代码:

func unpack(tarName string) error {
    path, _ := os.Getwd()
    //err := Untar(path+"/"+tarName, path+"/")
    fmt.Printf(path + "/" + tarName)
    cmd := exec.Command("tar", "-xfv", path+"/"+tarName)
    cmd.Stdout = os.Stdout
    err := cmd.Run()
    return err
}
英文:

I have a tar.gz file and i need to unpack it using golang.
I've tried libs like "archive/tar" but they gave me error: archive/tar: invalid tar header. Now my idea was to use exec to run tar command and unpack tarball, but it always exits with code 2.

My code:

func unpack(tarName string) error {
path, _ := os.Getwd()
//err := Untar(path+"/"+tarName, path+"/")
fmt.Printf(path + "/" + tarName)
cmd := exec.Command("tar", "-xfv", path+"/"+tarName)
cmd.Stdout = os.Stdout
err := cmd.Run()
return err
}

答案1

得分: 1

如果您想压缩tar.gz文件,首先需要解压缩gzip。


import (
    "archive/tar"
    "compress/gzip"
    "fmt"
    "io"
    "log"
    "os"
)

func ExtractTarGz(gzipStream io.Reader) {
    uncompressedStream, err := gzip.NewReader(gzipStream)
    if err != nil {
        log.Fatal("ExtractTarGz: NewReader failed")
    }

    tarReader := tar.NewReader(uncompressedStream)

    for true {
        header, err := tarReader.Next()

        if err == io.EOF {
            break
        }

        if err != nil {
            log.Fatalf("ExtractTarGz: Next() failed: %s", err.Error())
        }

        switch header.Typeflag {
        case tar.TypeDir:
            if err := os.Mkdir(header.Name, 0755); err != nil {
                log.Fatalf("ExtractTarGz: Mkdir() failed: %s", err.Error())
            }
        case tar.TypeReg:
            outFile, err := os.Create(header.Name)
            if err != nil {
                log.Fatalf("ExtractTarGz: Create() failed: %s", err.Error())
            }
            if _, err := io.Copy(outFile, tarReader); err != nil {
                log.Fatalf("ExtractTarGz: Copy() failed: %s", err.Error())
            }
            outFile.Close()

        default:
            log.Fatalf(
                "ExtractTarGz: uknown type: %s in %s",
                header.Typeflag,
                header.Name)
        }

    }
}
func main() {
    r, err := os.Open("./file.tar.gz")
    if err != nil {
        fmt.Println("error")
    }
    ExtractTarGz(r)
}
英文:

If you are trying to compress tar.gz file you need first to decompress gzip.


import (
    "archive/tar"
    "compress/gzip"
    "fmt"
    "io"
    "log"
    "os"
)

func ExtractTarGz(gzipStream io.Reader) {
    uncompressedStream, err := gzip.NewReader(gzipStream)
    if err != nil {
        log.Fatal("ExtractTarGz: NewReader failed")
    }

    tarReader := tar.NewReader(uncompressedStream)

    for true {
        header, err := tarReader.Next()

        if err == io.EOF {
            break
        }

        if err != nil {
            log.Fatalf("ExtractTarGz: Next() failed: %s", err.Error())
        }

        switch header.Typeflag {
        case tar.TypeDir:
            if err := os.Mkdir(header.Name, 0755); err != nil {
                log.Fatalf("ExtractTarGz: Mkdir() failed: %s", err.Error())
            }
        case tar.TypeReg:
            outFile, err := os.Create(header.Name)
            if err != nil {
                log.Fatalf("ExtractTarGz: Create() failed: %s", err.Error())
            }
            if _, err := io.Copy(outFile, tarReader); err != nil {
                log.Fatalf("ExtractTarGz: Copy() failed: %s", err.Error())
            }
            outFile.Close()

        default:
            log.Fatalf(
                "ExtractTarGz: uknown type: %s in %s",
                header.Typeflag,
                header.Name)
        }

    }
}
func main() {
    r, err := os.Open("./file.tar.gz")
    if err != nil {
        fmt.Println("error")
    }
    ExtractTarGz(r)
}



</details>



huangapple
  • 本文由 发表于 2022年5月16日 07:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/72252992.html
匿名

发表评论

匿名网友

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

确定