2023-11-04 16:33:24 +00:00
|
|
|
package sys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"compress/bzip2"
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ulikunitz/xz"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Xtract struct {
|
|
|
|
dir string
|
|
|
|
reader *bufio.Reader
|
|
|
|
bar *io.Writer
|
|
|
|
outFile string
|
|
|
|
withBar bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewXtract(dir string, outFile string) *Xtract {
|
|
|
|
return &Xtract{dir: dir, outFile: outFile, withBar: false}
|
|
|
|
}
|
|
|
|
|
2023-11-12 01:09:37 +00:00
|
|
|
func (x *Xtract) AddBar(bar io.Writer) {
|
|
|
|
x.bar = &bar
|
2023-11-04 16:33:24 +00:00
|
|
|
x.withBar = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Xtract) setCompressedFile(dumpName string) {
|
|
|
|
path := fmt.Sprintf("%s/%s", x.dir, dumpName)
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
x.reader = bufio.NewReader(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Xtract) writeUncompressedData(out *bytes.Buffer) bool {
|
|
|
|
err := os.WriteFile(x.outFile, out.Bytes(), 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Xtract) UnGzip(dumpName string) bool {
|
|
|
|
x.setCompressedFile(dumpName)
|
|
|
|
var out bytes.Buffer
|
|
|
|
r, err := gzip.NewReader(x.reader)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if x.withBar {
|
2023-11-12 14:31:49 +00:00
|
|
|
_, err = io.Copy(io.MultiWriter(&out, *x.bar), r)
|
2023-11-04 16:33:24 +00:00
|
|
|
} else {
|
2023-11-12 14:31:49 +00:00
|
|
|
_, err = io.Copy(&out, r)
|
2023-11-04 16:33:24 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// if x.withBar {
|
|
|
|
// x.bar.Clear()
|
|
|
|
// }
|
|
|
|
return x.writeUncompressedData(&out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Xtract) UnBz2(dumpName string) bool {
|
|
|
|
x.setCompressedFile(dumpName)
|
|
|
|
var out bytes.Buffer
|
|
|
|
r := bzip2.NewReader(x.reader)
|
|
|
|
var err error
|
|
|
|
if x.withBar {
|
|
|
|
_, err = io.Copy(io.MultiWriter(&out, *x.bar), r)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(&out, r)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return x.writeUncompressedData(&out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Xtract) UnXz(dumpName string) bool {
|
|
|
|
x.setCompressedFile(dumpName)
|
|
|
|
var out bytes.Buffer
|
|
|
|
r, err := xz.NewReader(x.reader)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if x.withBar {
|
|
|
|
_, err = io.Copy(io.MultiWriter(&out, *x.bar), r)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(&out, r)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return x.writeUncompressedData(&out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Xtract) UnZip(dumpName string) bool {
|
|
|
|
var done bool = false
|
|
|
|
path := fmt.Sprintf("%s/%s", x.dir, dumpName)
|
|
|
|
r, err := zip.OpenReader(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
if len(r.File) > 0 {
|
|
|
|
rc, err := r.File[0].Open()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
var out bytes.Buffer
|
|
|
|
if x.withBar {
|
|
|
|
_, err = io.Copy(io.MultiWriter(&out, *x.bar), rc)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(&out, rc)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
rc.Close()
|
|
|
|
done = x.writeUncompressedData(&out)
|
|
|
|
}
|
|
|
|
return done
|
|
|
|
}
|