Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include tar.xz (de)compression #14

Closed
cocodrino opened this issue Oct 12, 2016 · 3 comments
Closed

include tar.xz (de)compression #14

cocodrino opened this issue Oct 12, 2016 · 3 comments

Comments

@cocodrino
Copy link

Hi..I think than it could be possible using https://godoc.org/xi2.org/x/xz

package main

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

     "xi2.org/x/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()
}

can u check this?..thanks!

@mholt
Copy link
Owner

mholt commented Oct 13, 2016

Cool! I can look into it. Is there a pure Go package that does compression too? It'd be really great to have both if possible.

@cocodrino
Copy link
Author

cocodrino commented Oct 13, 2016

seems than the documentation is a bit old but if you go to the github project home you'll see than this even provides a command line utility for achieve this!! 😜 ...

@mholt
Copy link
Owner

mholt commented Nov 30, 2016

Done in cdc68dd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants