2021-01-16 20:20:30 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-05-01 00:18:45 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
2021-06-26 16:23:54 +08:00
|
|
|
"context"
|
2020-05-01 00:18:45 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-11-15 20:49:02 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/internal/fileinfo"
|
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
|
|
|
"github.com/gogf/gf/v2/text/gregex"
|
2020-05-01 00:18:45 +08:00
|
|
|
)
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// ZipPathWriter compresses `paths` to `writer` using zip compressing algorithm.
|
|
|
|
// The unnecessary parameter `prefix` indicates the path prefix for zip file.
|
2020-05-01 00:18:45 +08:00
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// Note that the parameter `paths` can be either a directory or a file, which
|
2020-05-01 00:18:45 +08:00
|
|
|
// supports multiple paths join with ','.
|
|
|
|
func zipPathWriter(paths string, writer io.Writer, prefix ...string) error {
|
|
|
|
zipWriter := zip.NewWriter(writer)
|
|
|
|
defer zipWriter.Close()
|
|
|
|
for _, path := range strings.Split(paths, ",") {
|
|
|
|
path = strings.TrimSpace(path)
|
|
|
|
if err := doZipPathWriter(path, "", zipWriter, prefix...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// doZipPathWriter compresses the file of given `path` and writes the content to `zipWriter`.
|
|
|
|
// The parameter `exclude` specifies the exclusive file path that is not compressed to `zipWriter`,
|
2020-05-01 00:18:45 +08:00
|
|
|
// commonly the destination zip file path.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The unnecessary parameter `prefix` indicates the path prefix for zip file.
|
2020-05-01 00:18:45 +08:00
|
|
|
func doZipPathWriter(path string, exclude string, zipWriter *zip.Writer, prefix ...string) error {
|
2020-05-14 20:32:01 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
files []string
|
|
|
|
)
|
2020-05-01 00:18:45 +08:00
|
|
|
path, err = gfile.Search(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if gfile.IsDir(path) {
|
|
|
|
files, err = gfile.ScanDir(path, "*", true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
files = []string{path}
|
|
|
|
}
|
|
|
|
headerPrefix := ""
|
|
|
|
if len(prefix) > 0 && prefix[0] != "" {
|
|
|
|
headerPrefix = prefix[0]
|
|
|
|
}
|
2021-08-02 19:58:04 +08:00
|
|
|
headerPrefix = strings.TrimRight(headerPrefix, `\/`)
|
2020-05-01 00:18:45 +08:00
|
|
|
if len(headerPrefix) > 0 && gfile.IsDir(path) {
|
|
|
|
headerPrefix += "/"
|
|
|
|
}
|
|
|
|
if headerPrefix == "" {
|
|
|
|
headerPrefix = gfile.Basename(path)
|
|
|
|
}
|
2021-08-02 19:58:04 +08:00
|
|
|
headerPrefix = strings.Replace(headerPrefix, `//`, `/`, -1)
|
2020-05-01 00:18:45 +08:00
|
|
|
for _, file := range files {
|
|
|
|
if exclude == file {
|
2021-06-26 16:23:54 +08:00
|
|
|
intlog.Printf(context.TODO(), `exclude file path: %s`, file)
|
2020-05-01 00:18:45 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-01-16 20:20:30 +08:00
|
|
|
err = zipFile(file, headerPrefix+gfile.Dir(file[len(path):]), zipWriter)
|
2020-05-01 00:18:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add all directories to zip archive.
|
|
|
|
if headerPrefix != "" {
|
|
|
|
var name string
|
|
|
|
path = headerPrefix
|
|
|
|
for {
|
2021-08-02 19:58:04 +08:00
|
|
|
name = strings.Replace(gfile.Basename(path), `\`, `/`, -1)
|
2021-01-16 20:20:30 +08:00
|
|
|
err = zipFileVirtual(
|
2020-05-01 00:18:45 +08:00
|
|
|
fileinfo.New(name, 0, os.ModeDir|os.ModePerm, time.Now()), path, zipWriter,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-02 19:58:04 +08:00
|
|
|
if path == `/` || !strings.Contains(path, `/`) {
|
2020-05-01 00:18:45 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
path = gfile.Dir(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// zipFile compresses the file of given `path` and writes the content to `zw`.
|
|
|
|
// The parameter `prefix` indicates the path prefix for zip file.
|
2020-05-01 00:18:45 +08:00
|
|
|
func zipFile(path string, prefix string, zw *zip.Writer) error {
|
2021-08-02 19:58:04 +08:00
|
|
|
prefix = strings.Replace(prefix, `//`, `/`, -1)
|
2020-05-01 00:18:45 +08:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
info, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
header, err := createFileHeader(info, prefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
header.Method = zip.Deflate
|
|
|
|
}
|
|
|
|
writer, err := zw.CreateHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
if _, err = io.Copy(writer, file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func zipFileVirtual(info os.FileInfo, path string, zw *zip.Writer) error {
|
|
|
|
header, err := createFileHeader(info, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
header.Name = path
|
2021-01-16 20:20:30 +08:00
|
|
|
if _, err = zw.CreateHeader(header); err != nil {
|
2020-05-01 00:18:45 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFileHeader(info os.FileInfo, prefix string) (*zip.FileHeader, error) {
|
|
|
|
header, err := zip.FileInfoHeader(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(prefix) > 0 {
|
|
|
|
header.Name = prefix + `/` + header.Name
|
2021-01-16 20:20:30 +08:00
|
|
|
header.Name = strings.Replace(header.Name, `\`, `/`, -1)
|
|
|
|
header.Name, _ = gregex.ReplaceString(`/{2,}`, `/`, header.Name)
|
2020-05-01 00:18:45 +08:00
|
|
|
}
|
|
|
|
return header, nil
|
|
|
|
}
|