Skip to content

Instantly share code, notes, and snippets.

@cdemers
Created May 4, 2017 15:59
Show Gist options
  • Save cdemers/95e65a599fa7c322c404e21e64416336 to your computer and use it in GitHub Desktop.
Save cdemers/95e65a599fa7c322c404e21e64416336 to your computer and use it in GitHub Desktop.
Sample code to read content from a .deb file in Go
package main
import (
"archive/tar"
"bytes"
"io"
"log"
"os"
"github.com/blakesmith/ar"
"github.com/ulikunitz/xz"
)
func main() {
arFile, err := os.Open("debianfile.deb")
if err != nil {
log.Fatal(err)
}
defer arFile.Close()
reader := ar.NewReader(arFile)
var buf bytes.Buffer
var buf2 bytes.Buffer
for {
header, err := reader.Next()
if err == io.EOF {
log.Fatal("The archive did not contain a data.tar.xz file.")
}
if err != nil {
log.Print(err.Error())
}
log.Print(header.Name)
if header.Name == "data.tar.xz" {
io.Copy(&buf, reader)
log.Print(buf)
break
}
}
//// Implementation for gz compression (instead of xz) ////
// gzf, err := gzip.NewReader(&buf)
// if err != nil {
// log.Fatal(err)
// }
// tarReader := tar.NewReader(gzf)
xzf, err := xz.NewReader(&buf)
if err != nil {
log.Fatal(err)
}
tarReader := tar.NewReader(xzf)
for {
header, err := tarReader.Next()
if err == io.EOF {
log.Fatal("The data.tar archive did not contain a ./requirements.json file.")
}
if err != nil {
log.Fatal(err)
}
log.Print(header.Name)
if header.Name == "./requirements.json" {
io.Copy(&buf2, tarReader)
break
}
}
log.Print(string(buf2.Bytes()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment