2023-09-18 15:58:52 +08:00
|
|
|
package archives
|
2023-09-18 15:51:20 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"io"
|
2023-09-18 15:58:52 +08:00
|
|
|
"lab.evlic.cn/go/pkg/files"
|
2023-09-18 15:51:20 +08:00
|
|
|
"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
|
|
|
|
}
|
2023-09-18 15:58:52 +08:00
|
|
|
err = files.FileSave(rd, dst)
|
2023-09-18 15:51:20 +08:00
|
|
|
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)
|
|
|
|
}
|