rpm

Package rpm implements the rpm package file format.
$ go get github.com/cavaliergopher/rpm
See the package documentation
or the examples programs in cmd/
to get started.
The following working example demonstrates how to extract files from an rpm
package. In this example, only the cpio format and xz compression are supported
which will cover most cases.
Implementations should consider additional formats and compressions algorithms,
as well as support for extracting irregular file types and configuring
permissions, uids and guids, etc.
package main
import (
"io"
"log"
"os"
"path/filepath"
"github.com/cavaliergopher/cpio"
"github.com/cavaliergopher/rpm"
"github.com/ulikunitz/xz"
)
func ExtractRPM(name string) {
f, err := os.Open(name)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pkg, err := rpm.Read(f)
if err != nil {
log.Fatal(err)
}
if compression := pkg.PayloadCompression(); compression != "xz" {
log.Fatalf("Unsupported compression: %s", compression)
}
xzReader, err := xz.NewReader(f)
if err != nil {
log.Fatal(err)
}
if format := pkg.PayloadFormat(); format != "cpio" {
log.Fatalf("Unsupported payload format: %s", format)
}
cpioReader := cpio.NewReader(xzReader)
for {
hdr, err := cpioReader.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if !hdr.Mode.IsRegular() {
continue
}
if dirName := filepath.Dir(hdr.Name); dirName != "" {
if err := os.MkdirAll(dirName, 0o755); err != nil {
log.Fatal(err)
}
}
outFile, err := os.Create(hdr.Name)
if err != nil {
log.Fatal(err)
}
if _, err := io.Copy(outFile, cpioReader); err != nil {
outFile.Close()
log.Fatal(err)
}
outFile.Close()
}
}