|
|
@ -2,64 +2,45 @@ package Archive |
|
|
|
|
|
|
|
import ( |
|
|
|
"archive/tar" |
|
|
|
"bufio" |
|
|
|
"bytes" |
|
|
|
"compress/gzip" |
|
|
|
"io" |
|
|
|
"io/fs" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
|
|
|
|
func UnGzip(source, target string) error { |
|
|
|
func ExtractArchive(source, target string) error { |
|
|
|
var ( |
|
|
|
reader, writer *os.File |
|
|
|
archive *gzip.Reader |
|
|
|
e error |
|
|
|
inFile *os.File |
|
|
|
gzipReader *gzip.Reader |
|
|
|
tarReader *tar.Reader |
|
|
|
e error |
|
|
|
) |
|
|
|
|
|
|
|
reader, e = os.Open(source) |
|
|
|
inFile, e = os.Open(source) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
} |
|
|
|
defer reader.Close() |
|
|
|
defer inFile.Close() |
|
|
|
|
|
|
|
archive, e = gzip.NewReader(reader) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
} |
|
|
|
defer archive.Close() |
|
|
|
|
|
|
|
target = filepath.Join(target, archive.Name) |
|
|
|
|
|
|
|
writer, e = os.Create(target) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
} |
|
|
|
defer writer.Close() |
|
|
|
gzipReader, e = gzip.NewReader(inFile) |
|
|
|
defer gzipReader.Close() |
|
|
|
tarReader = tar.NewReader(gzipReader) |
|
|
|
|
|
|
|
_, e = io.Copy(writer, archive) |
|
|
|
return e |
|
|
|
return extractFromArchive(tarReader, target) |
|
|
|
} |
|
|
|
|
|
|
|
func Untar(tarball, target string) error { |
|
|
|
func extractFromArchive(tarReader *tar.Reader, target string) error { |
|
|
|
var ( |
|
|
|
reader *os.File |
|
|
|
tarReader *tar.Reader |
|
|
|
header *tar.Header |
|
|
|
info fs.FileInfo |
|
|
|
file *os.File |
|
|
|
path string |
|
|
|
e error |
|
|
|
header *tar.Header |
|
|
|
info fs.FileInfo |
|
|
|
file *os.File |
|
|
|
path, basePath string |
|
|
|
e error |
|
|
|
) |
|
|
|
|
|
|
|
reader, e = os.Open(tarball) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
} |
|
|
|
|
|
|
|
defer reader.Close() |
|
|
|
tarReader = tar.NewReader(reader) |
|
|
|
|
|
|
|
for { |
|
|
|
header, e = tarReader.Next() |
|
|
|
if e == io.EOF { |
|
|
@ -72,6 +53,10 @@ func Untar(tarball, target string) error { |
|
|
|
path = filepath.Join(target, header.Name) |
|
|
|
|
|
|
|
info = header.FileInfo() |
|
|
|
if filepath.Base(info.Name()) == "manifest.yml" { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
if info.IsDir() { |
|
|
|
e = os.MkdirAll(path, info.Mode()) |
|
|
|
if e != nil { |
|
|
@ -80,7 +65,20 @@ func Untar(tarball, target string) error { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
file, e = os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode()) |
|
|
|
basePath, e = filepath.Abs(filepath.Dir(path)) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
} |
|
|
|
|
|
|
|
_, e = os.Stat(basePath) |
|
|
|
if os.IsNotExist(e) { |
|
|
|
e = os.MkdirAll(basePath, info.Mode()) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
file, e = os.OpenFile(path, os.O_CREATE|os.O_RDWR, info.Mode()) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
} |
|
|
@ -94,23 +92,60 @@ func Untar(tarball, target string) error { |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func UntarGzip(source, target string) error { |
|
|
|
func ExtractManifestFile(source string) (string, error) { |
|
|
|
var ( |
|
|
|
tarPath string = strings.ReplaceAll(source, ".gz", "") |
|
|
|
e error |
|
|
|
inFile *os.File |
|
|
|
gzipReader *gzip.Reader |
|
|
|
tarReader *tar.Reader |
|
|
|
e error |
|
|
|
) |
|
|
|
|
|
|
|
e = UnGzip(source, tarPath) |
|
|
|
inFile, e = os.Open(source) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
return "", e |
|
|
|
} |
|
|
|
defer inFile.Close() |
|
|
|
|
|
|
|
e = Untar(tarPath, target) |
|
|
|
if e != nil { |
|
|
|
return e |
|
|
|
gzipReader, e = gzip.NewReader(inFile) |
|
|
|
defer gzipReader.Close() |
|
|
|
tarReader = tar.NewReader(gzipReader) |
|
|
|
|
|
|
|
return extractManifestFromArchive(tarReader) |
|
|
|
} |
|
|
|
|
|
|
|
func extractManifestFromArchive(tarReader *tar.Reader) (string, error) { |
|
|
|
var ( |
|
|
|
header *tar.Header |
|
|
|
info fs.FileInfo |
|
|
|
manifestWriter *bufio.Writer |
|
|
|
manifestBytes bytes.Buffer |
|
|
|
e error |
|
|
|
) |
|
|
|
|
|
|
|
for { |
|
|
|
header, e = tarReader.Next() |
|
|
|
if e == io.EOF { |
|
|
|
break |
|
|
|
} |
|
|
|
if e != nil { |
|
|
|
return manifestBytes.String(), e |
|
|
|
} |
|
|
|
|
|
|
|
info = header.FileInfo() |
|
|
|
if filepath.Base(info.Name()) != "manifest.yml" { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
manifestWriter = bufio.NewWriter(&manifestBytes) |
|
|
|
|
|
|
|
_, e = io.Copy(manifestWriter, tarReader) |
|
|
|
if e != nil { |
|
|
|
return manifestBytes.String(), e |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return os.Remove(tarPath) |
|
|
|
return manifestBytes.String(), nil |
|
|
|
} |