func writeZipFile(dir string, targetFile *os.File) (err error) { isEmpty, err := fileutils.IsDirEmpty(dir) if err != nil { return } if isEmpty { err = errors.New("Directory is empty") return } writer := zip.NewWriter(targetFile) defer writer.Close() err = WalkAppFiles(dir, func(fileName string, fullPath string) (err error) { fileInfo, err := os.Stat(fullPath) if err != nil { return err } header, err := zip.FileInfoHeader(fileInfo) header.Name = filepath.ToSlash(fileName) if err != nil { return err } zipFilePart, err := writer.CreateHeader(header) err = fileutils.CopyPathToWriter(fullPath, zipFilePart) return }) return }
func AppFilesInDir(dir string) (appFiles []AppFileFields, err error) { dir, err = filepath.Abs(dir) if err != nil { return } err = WalkAppFiles(dir, func(fileName string, fullPath string) (err error) { fileInfo, err := os.Lstat(fullPath) if err != nil { return } size := fileInfo.Size() h := sha1.New() err = fileutils.CopyPathToWriter(fullPath, h) if err != nil { return } sha1Bytes := h.Sum(nil) sha1 := fmt.Sprintf("%x", sha1Bytes) appFiles = append(appFiles, AppFileFields{ Path: fileName, Sha1: sha1, Size: size, }) return }) return }
func (zipper ApplicationZipper) Zip(dirOrZipFile string, targetFile *os.File) (err error) { if zipper.IsZipFile(dirOrZipFile) { err = fileutils.CopyPathToWriter(dirOrZipFile, targetFile) } else { err = writeZipFile(dirOrZipFile, targetFile) } targetFile.Seek(0, os.SEEK_SET) return }
func writeZipFile(dir string, targetFile *os.File) (err error) { isEmpty, err := fileutils.IsDirEmpty(dir) if err != nil { return } if isEmpty { err = errors.New("Directory is empty") return } writer := zip.NewWriter(targetFile) defer writer.Close() err = walkAppFiles(dir, func(fileName string, fullPath string) (err error) { zipFilePart, err := writer.Create(fileName) err = fileutils.CopyPathToWriter(fullPath, zipFilePart) return }) return }