Ejemplo n.º 1
0
// packDir packs a directory and its subdirectories and files
// recursively to zip.Writer.
func packDir(srcPath string, recPath string, zw *zip.Writer, fn cae.HookFunc) error {
	dir, err := os.Open(srcPath)
	if err != nil {
		return err
	}
	defer dir.Close()

	fis, err := dir.Readdir(0)
	if err != nil {
		return err
	}
	for _, fi := range fis {
		if cae.IsFilter(fi.Name()) {
			continue
		}
		curPath := srcPath + "/" + fi.Name()
		tmpRecPath := filepath.Join(recPath, fi.Name())
		if err = fn(curPath, fi); err != nil {
			continue
		}

		if fi.IsDir() {
			if err = packFile(srcPath, tmpRecPath, zw, fi); err != nil {
				return err
			}
			err = packDir(curPath, tmpRecPath, zw, fn)
		} else {
			err = packFile(curPath, tmpRecPath, zw, fi)
		}
		if err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 2
0
Archivo: tz.go Proyecto: xuewuhen/cae
// AddFile adds a file entry to TzArchive.
func (tz *TzArchive) AddFile(fileName, absPath string) error {
	if cae.IsFilter(absPath) {
		return nil
	}

	si, err := os.Lstat(absPath)
	if err != nil {
		return err
	}

	target := ""
	if si.Mode()&os.ModeSymlink != 0 {
		target, err = os.Readlink(absPath)
		if err != nil {
			return err
		}
	}

	file := new(File)
	file.Header, err = tar.FileInfoHeader(si, target)
	if err != nil {
		return err
	}
	file.Name = fileName
	file.absPath = absPath

	tz.AddEmptyDir(path.Dir(fileName))

	isExist := false
	for _, f := range tz.files {
		if fileName == f.Name {
			f = file
			isExist = true
			break
		}
	}
	if !isExist {
		tz.files = append(tz.files, file)
	}

	tz.updateStat()
	return nil
}
Ejemplo n.º 3
0
Archivo: zip.go Proyecto: xuewuhen/cae
// AddFile adds a file entry to ZipArchive.
func (z *ZipArchive) AddFile(fileName, absPath string) error {
	if cae.IsFilter(absPath) {
		return nil
	}

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

	fi, err := f.Stat()
	if err != nil {
		return err
	}

	file := new(File)
	file.FileHeader, err = zip.FileInfoHeader(fi)
	if err != nil {
		return err
	}
	file.Name = fileName
	file.absPath = absPath

	z.AddEmptyDir(path.Dir(fileName))

	isExist := false
	for _, f := range z.files {
		if fileName == f.Name {
			f = file
			isExist = true
			break
		}
	}
	if !isExist {
		z.files = append(z.files, file)
	}

	z.updateStat()
	return nil
}
Ejemplo n.º 4
0
// packDir packs a directory and its subdirectories and files
// recursively to zip.Writer.
func packDir(srcPath string, recPath string, tw *tar.Writer, fn cae.HookFunc) error {
	dir, err := os.Open(srcPath)
	if err != nil {
		return err
	}
	defer dir.Close()

	// Get file info slice
	fis, err := dir.Readdir(0)
	if err != nil {
		return err
	}

	for _, fi := range fis {
		if cae.IsFilter(fi.Name()) {
			continue
		}
		// Append path
		curPath := srcPath + "/" + fi.Name()
		tmpRecPath := filepath.Join(recPath, fi.Name())
		if err = fn(curPath, fi); err != nil {
			continue
		}

		// Check it is directory or file
		if fi.IsDir() {
			if err = packFile(srcPath, tmpRecPath, tw, fi); err != nil {
				return err
			}

			err = packDir(curPath, tmpRecPath, tw, fn)
		} else {
			err = packFile(curPath, tmpRecPath, tw, fi)
		}
		if err != nil {
			return err
		}
	}
	return nil
}