golang zip压缩和解压缩

package ziputil

import (
	"archive/zip"
	"errors"
	"io"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"strings"
)

func IsZip(src string) (bool, error) {
	f, err := os.Stat(src)
	if err != nil {
		return false, nil
	}

	if f.IsDir() {
		return false, nil
	}

	return ExtZip == filepath.Ext(f.Name()), nil

}

func IsDir(src string) (bool, error) {
	f, err := os.Stat(src)
	if err != nil {
		return false, err
	}

	return f.IsDir(), nil
}

func IsFileExist(filePath string) (bool, error) {
	_, err := os.Stat(filePath)
	if err == nil {
		return true, nil
	}

	if os.IsNotExist(err) {
		return false, nil
	}

	return false, err
}

func GetSrcZips(src string) ([]string, error) {
	files, err := ioutil.ReadDir(src)
	if err != nil {
		return nil, err
	}
	fileNames := []string{}
	for _, f := range files {
		ext := filepath.Ext(f.Name())
		if ext != ExtZip {
			continue
		}

		fileNames = append(fileNames, strings.TrimSuffix(f.Name(), ext))
	}

	return fileNames, nil
}

func Compress(srcDir, dstZipPath string) error {
	if filepath.Ext(filepath.Base(dstZipPath)) != ExtZip {
		return errors.New("not a zip file")
	}

	dstDir := filepath.Dir(dstZipPath)
	isDstDir, err := IsDir(dstDir)
	if err != nil {
		return err
	}
	if !isDstDir {
		return errors.New("dstDir is not a directory")
	}

	isSrcDir, err := IsDir(srcDir)
	if err != nil {
		return err
	}
	if !isSrcDir {
		return errors.New("srcDirs is not a directory")
	}

	f, err := os.Create(dstZipPath)
	if err != nil {
		return err
	}
	defer f.Close()

	zw := zip.NewWriter(f)
	defer zw.Close()

	files, err := ioutil.ReadDir(srcDir)
	if err != nil {
		return err
	}

	for _, fi := range files {
		if err := compress(fi, srcDir, "", zw); err != nil {
			return err
		}
	}

	return nil
}

func compress(fi os.FileInfo, fileDir string, subName string, zw *zip.Writer) error {
	if fi.IsDir() {
		fileDir := filepath.Join(subName, fi.Name())
		if subName != "" {
			subName = filepath.Join(subName, fi.Name())
		} else {
			subName = fi.Name()
		}

		header, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		header.Name = subName + "/"

		_, err = zw.CreateHeader(header)
		if err != nil {
			return err
		}

		files, err := ioutil.ReadDir(fileDir)
		if err != nil {
			return err
		}
		for _, fi := range files {
			if err := compress(fi, fileDir, subName, zw); err != nil {
				return err
			}
		}
	} else {
		filePath := filepath.Join(fileDir, fi.Name())
		f, err := os.Open(filePath)
		if err != nil {
			return err
		}
		defer f.Close()

		header, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		if subName != "" {
			header.Name = filepath.Join(subName, fi.Name())
		}
		writer, err := zw.CreateHeader(header)
		if err != nil {
			return err
		}
		_, err = io.Copy(writer, f)
		if err != nil {
			return err
		}
	}

	return nil
}

const (
	ExtZip = ".zip"
	// maxFileCount 压缩文件最大文件数
	maxFileCount = 1500
	// singleFileMaxSize 单个文件解压和总文件解压不能超过10G
	singleFileMaxSize uint64 = 10737418240
	fileMaxSize       int64  = 10737418240
)

func UnCompress(src, dst string) error {
	isZip, err := IsZip(src)
	if err != nil {
		return err
	}
	if !isZip {
		return errors.New("src is not a zip file")
	}

	isDir, err := IsDir(dst)
	if err != nil {
		return err
	}
	if !isDir {
		return errors.New("dst is not a directory")
	}

	srcReader, err := zip.OpenReader(src)
	if err != nil {
		return err
	}
	defer srcReader.Close()

	if len(srcReader.File) > maxFileCount {
		return errors.New("zip file have too many files")
	}

	for _, f := range srcReader.File {
		// safety requirements
		if strings.Contains(f.Name, "./") ||
			strings.Contains(f.Name, ".\\") ||
			strings.Contains(f.Name, "..") {
		}
		if f.UncompressedSize64 > singleFileMaxSize {
			return errors.New("single file exceeds the maximum size")
		}
	}
	err = writeReader(srcReader, dst)
	if err != nil {
		return err
	}

	return nil
}

func writeReader(srcReader *zip.ReadCloser, dst string) error {
	var totalSize int64
	for _, f := range srcReader.File {
		if totalSize > fileMaxSize {
			return errors.New("the total size has exceeds the upper limit")
		}
		fileName := f.Name
		targetFilePath := filepath.Join(dst, fileName)

		if f.FileInfo().IsDir() {
			err := os.MkdirAll(targetFilePath, f.Mode())
			if err != nil {
				return err
			}
			continue
		}

		isExist, err := IsFileExist(targetFilePath)
		if err != nil {
			return err
		}
		if isExist {
			return errors.New("the target path has exist")
		}

		if err := os.MkdirAll(path.Dir(targetFilePath), 0700); err != nil {
			return err
		}

		writeSize, err := writeFile(f, targetFilePath)
		if err != nil {
			return err
		}
		totalSize += writeSize
	}

	return nil
}

func writeFile(f *zip.File, targetFilePath string) (int64, error) {
	targetFile, err := os.Create(targetFilePath)
	if err != nil {
		return 0, err
	}
	defer targetFile.Close()
	file, err := f.Open()
	if err != nil {
		return 0, err
	}
	defer file.Close()

	writeSize, err := io.Copy(targetFile, file)
	if err != nil {
		return 0, err
	}

	return writeSize, nil
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值