pkg/archive: add function to extract archive

This will be used to test archive integrity by extracting it
This commit is contained in:
Clayton Craft
2021-08-10 13:21:42 -07:00
parent 766da6a0dc
commit 2633f0bde9

View File

@@ -4,11 +4,12 @@ package archive
import ( import (
"bytes" "bytes"
"io"
"log" "log"
"os" "os"
"strings" "strings"
"io"
"encoding/hex" "encoding/hex"
"io/ioutil"
"path/filepath" "path/filepath"
"compress/flate" "compress/flate"
"crypto/sha256" "crypto/sha256"
@@ -169,13 +170,50 @@ func (archive *Archive) AddFile(file string, dest string) error {
return nil return nil
} }
func (archive *Archive) writeCompressed(path string, mode os.FileMode) error { func extract(path string, dest string) error {
tFile, err := renameio.TempFile("", path) tDir, err := ioutil.TempDir("", "archive-extract")
if err != nil { if err != nil {
return err return err
} }
defer tFile.Cleanup() defer os.RemoveAll(tDir)
srcFd, err := os.Open(path)
if err != nil {
return err
}
defer srcFd.Close()
// TODO: support more compression types
gz, err := pgzip.NewReader(srcFd)
cpioArchive := cpio.NewReader(gz)
for {
hdr, err := cpioArchive.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
destPath := filepath.Join(dest, hdr.Name)
if hdr.Mode.IsDir() {
os.MkdirAll(destPath, 0755)
} else {
destFd, err := os.Create(destPath)
if err != nil {
return err
}
defer destFd.Close()
if _, err := io.Copy(destFd, cpioArchive); err != nil {
return err
}
}
}
return nil
}
func (archive *Archive) writeCompressed(path string, mode os.FileMode) error {
// TODO: support other compression formats, based on deviceinfo // TODO: support other compression formats, based on deviceinfo
gz, err := pgzip.NewWriterLevel(tFile, flate.BestSpeed) gz, err := pgzip.NewWriterLevel(tFile, flate.BestSpeed)
if err != nil { if err != nil {