mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-02-25 07:14:43 +08:00
feat: 优化 zip 文件压缩/解压 (#3106)
Refs https://github.com/1Panel-dev/1Panel/issues/3068
This commit is contained in:
parent
4ed131cde7
commit
82881273ea
1 changed files with 108 additions and 56 deletions
|
@ -418,7 +418,7 @@ func getFormat(cType CompressType) archiver.CompressedArchive {
|
|||
case SdkTarGz:
|
||||
format.Compression = archiver.Gz{}
|
||||
format.Archival = archiver.Tar{}
|
||||
case SdkZip:
|
||||
case SdkZip, Zip:
|
||||
format.Archival = archiver.Zip{
|
||||
Compression: zip.Deflate,
|
||||
}
|
||||
|
@ -457,6 +457,10 @@ func (f FileOp) Compress(srcRiles []string, dst string, name string, cType Compr
|
|||
|
||||
switch cType {
|
||||
case Zip:
|
||||
if err := ZipFile(files, out); err == nil {
|
||||
return nil
|
||||
}
|
||||
_ = f.DeleteFile(dstFile)
|
||||
return NewZipArchiver().Compress(srcRiles, dstFile)
|
||||
default:
|
||||
err = format.Archive(context.Background(), out, files)
|
||||
|
@ -481,15 +485,7 @@ func decodeGBK(input string) (string, error) {
|
|||
return decoded, nil
|
||||
}
|
||||
|
||||
func (f FileOp) Decompress(srcFile string, dst string, cType CompressType) error {
|
||||
switch cType {
|
||||
case Tar, Zip:
|
||||
shellArchiver, err := NewShellArchiver(cType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return shellArchiver.Extract(srcFile, dst)
|
||||
default:
|
||||
func (f FileOp) decompressWithSDK(srcFile string, dst string, cType CompressType) error {
|
||||
format := getFormat(cType)
|
||||
handler := func(ctx context.Context, archFile archiver.File) error {
|
||||
info := archFile.FileInfo
|
||||
|
@ -542,6 +538,19 @@ func (f FileOp) Decompress(srcFile string, dst string, cType CompressType) error
|
|||
}
|
||||
return format.Extract(context.Background(), input, nil, handler)
|
||||
}
|
||||
|
||||
func (f FileOp) Decompress(srcFile string, dst string, cType CompressType) error {
|
||||
if err := f.decompressWithSDK(srcFile, dst, cType); err != nil {
|
||||
if cType == Tar || cType == Zip {
|
||||
shellArchiver, err := NewShellArchiver(cType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return shellArchiver.Extract(srcFile, dst)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FileOp) Backup(srcFile string) (string, error) {
|
||||
|
@ -577,3 +586,46 @@ func (f FileOp) CopyAndBackup(src string) (string, error) {
|
|||
}
|
||||
return backupPath, nil
|
||||
}
|
||||
|
||||
func ZipFile(files []archiver.File, dst afero.File) error {
|
||||
zw := zip.NewWriter(dst)
|
||||
defer zw.Close()
|
||||
|
||||
for _, file := range files {
|
||||
hdr, err := zip.FileInfoHeader(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hdr.Name = file.NameInArchive
|
||||
if file.IsDir() {
|
||||
if !strings.HasSuffix(hdr.Name, "/") {
|
||||
hdr.Name += "/"
|
||||
}
|
||||
hdr.Method = zip.Store
|
||||
}
|
||||
w, err := zw.CreateHeader(hdr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if file.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
if file.LinkTarget != "" {
|
||||
_, err = w.Write([]byte(filepath.ToSlash(file.LinkTarget)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
fileReader, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(w, fileReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue