123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package fileutils
- import (
- "archive/zip"
- "bytes"
- "io"
- "log"
- "os"
- "path/filepath"
- "strings"
- )
- func PathExists(path string) bool {
- _, err := os.Stat(path)
- if err == nil {
- return true
- }
- if os.IsNotExist(err) {
- return false
- }
- return false
- }
- func GetDirFiles(dir string) ([]string, error) {
- dirList, err := os.ReadDir(dir)
- if err != nil {
- return nil, err
- }
- filesRet := make([]string, 0)
- for _, file := range dirList {
- if file.IsDir() {
- files, err := GetDirFiles(filepath.Join(dir, file.Name()))
- if err != nil {
- return nil, err
- }
- filesRet = append(filesRet, files...)
- } else {
- filesRet = append(filesRet, filepath.Join(dir, file.Name()))
- }
- }
- return filesRet, nil
- }
- func ZipDir(dirPath string, savePath string) error {
- archive, err := os.Create(savePath)
- if err != nil {
- return err
- }
- defer func(archive *os.File) {
- err := archive.Close()
- if err != nil {
- log.Println(err)
- return
- }
- }(archive)
- zipWriter := zip.NewWriter(archive)
- defer func(zipWriter *zip.Writer) {
- err := zipWriter.Close()
- if err != nil {
- log.Println(err)
- return
- }
- }(zipWriter)
- err = filepath.Walk(dirPath,
- func(path string, f os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if f.IsDir() {
- return nil
- }
- zipPath := strings.TrimPrefix(path, dirPath)
- err = writeZipFile(path, zipPath, zipWriter)
- if err != nil {
- return err
- }
- return nil
- })
- if err != nil {
- return err
- }
- return nil
- }
- func UnzipFile(srcFilePath string, destDir string) error {
- srcFile, err := os.Open(srcFilePath)
- if err != nil {
- return err
- }
- defer func() {
- err := srcFile.Close()
- if err != nil {
- log.Println(err)
- return
- }
- }()
- srcFileInfo, err := srcFile.Stat()
- if err != nil {
- return err
- }
- return unzip(srcFile, srcFileInfo.Size(), destDir)
- }
- func UnzipBytes(zipFileBytes []byte, destDir string) error {
- return unzip(bytes.NewReader(zipFileBytes), int64(len(zipFileBytes)), destDir)
- }
- func writeZipFile(filePath string, zipPath string, zipWriter *zip.Writer) error {
- file, err := os.Open(filePath)
- if err != nil {
- return err
- }
- defer func() {
- err := file.Close()
- if err != nil {
- log.Println(err)
- return
- }
- }()
- writer, err := zipWriter.Create(zipPath)
- if err != nil {
- return err
- }
- _, err = io.Copy(writer, file)
- if err != nil {
- return err
- }
- return nil
- }
- func unzip(r io.ReaderAt, size int64, destDir string) error {
- zipReader, err := zip.NewReader(r, size)
- if err != nil {
- return err
- }
- for _, f := range zipReader.File {
- fPath := filepath.Join(destDir, f.Name)
- if f.FileInfo().IsDir() {
- if err := os.MkdirAll(fPath, os.ModePerm); err != nil {
- return err
- }
- } else {
- if err := os.MkdirAll(filepath.Dir(fPath), os.ModePerm); err != nil {
- return err
- }
- inFile, err := f.Open()
- if err != nil {
- return err
- }
- outFile, err := os.OpenFile(fPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
- if err != nil {
- return err
- }
- if _, err = io.Copy(outFile, inFile); err != nil {
- return err
- }
- if err = inFile.Close(); err != nil {
- return err
- }
- if err = outFile.Close(); err != nil {
- return err
- }
- }
- }
- return nil
- }
|