84 lines
1.4 KiB
Go
84 lines
1.4 KiB
Go
package archives
|
|
|
|
import (
|
|
"archive/tar"
|
|
"archive/zip"
|
|
"go.uber.org/zap"
|
|
"io"
|
|
"lab.evlic.cn/go/pkg/files"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type ArchiveFile interface {
|
|
Open() (io.ReadCloser, error)
|
|
}
|
|
|
|
func DecZIP(src, dst string) error {
|
|
r, err := zip.OpenReader(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
|
|
var (
|
|
name, dstFile string
|
|
)
|
|
for _, file := range r.File {
|
|
if file.FileInfo().IsDir() {
|
|
continue
|
|
}
|
|
name = file.FileHeader.Name
|
|
dstFile = filepath.Join(dst, name)
|
|
decompressSave(name, dstFile, file)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func DecTar(src, dst string) error {
|
|
fsrc, err := os.Open(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer fsrc.Close()
|
|
|
|
r := tar.NewReader(fsrc)
|
|
var (
|
|
//name, dstFile string
|
|
)
|
|
|
|
for file, err := r.Next(); err != io.EOF; file, err = r.Next() {
|
|
if file.FileInfo().IsDir() {
|
|
continue
|
|
}
|
|
//file.Name
|
|
|
|
//create, err := os.Create("xx")
|
|
//io.Copy(create, file)
|
|
//name = file.Name
|
|
//dstFile = filepath.Join(dst, name)
|
|
//decompressSave(name, dstFile, file)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func decompressSave(name, dst string, file ArchiveFile) {
|
|
var (
|
|
err error
|
|
rd io.ReadCloser
|
|
)
|
|
rd, err = file.Open()
|
|
if err != nil {
|
|
zap.S().Infof("\tread %#q err %#v: %q\n", name, err, err.Error())
|
|
rd.Close()
|
|
return
|
|
}
|
|
err = files.FileSave(rd, dst)
|
|
rd.Close()
|
|
if err != nil {
|
|
zap.S().Infof("\tsave %#q err %#v: %q\n", name, err, err.Error())
|
|
return
|
|
}
|
|
zap.S().Debugf("%q\n", name)
|
|
}
|