Golang archive-tar.Writer类(方法)实例源码

下面列出了Golang archive-tar.Writer 类(方法)源码代码实例,从而了解它的用法。

作者:vizidri    项目:gotarpac   
/*
handle		- Pointer to a Tar file being built
rootPath	- Path to read the contents from
relPath		- Path to write these files into
*/
func writeFile(handle *tar.Writer, rootPath, relPath string) error {
	var file *os.File
	var stat os.FileInfo
	var buffer []byte
	var err error
	if file, err = os.OpenFile(rootPath, os.O_RDONLY, os.ModePerm); err != nil {
		return err
	}
	if stat, err = file.Stat(); err != nil {
		return err
	}
	file.Close()
	header := &tar.Header{
		Name: relPath,
		Size: stat.Size(),
	}
	if err := handle.WriteHeader(header); err != nil {
		return err
	}
	if buffer, err = ioutil.ReadFile(rootPath); err != nil {
		return err
	}
	if _, err := handle.Write(buffer); err != nil {
		return err
	}
	return nil
}

作者:tosha    项目:gob   
func (c *PackCmd) packLib(writer *tar.Writer, dir string) error {
	// these should be of the form $GOOS_$GOARCH
	name := fmt.Sprintf("pkg/%s/%s%s", dir, c.Paths.PkgName, GO_LIB_EXT)
	fname := os.ExpandEnv("$GOPATH/") + name
	c.log.Logv("Looking for %s", fname)
	file, err := os.Open(fname)
	if err != nil {
		return err
	}

	defer file.Close()

	body, err := ioutil.ReadAll(file)
	if err != nil {
		panic(err)
	}

	header := tar.Header{
		Name: name,
		Mode: 0444,
		Size: int64(len(body)),
	}

	writer.WriteHeader(&header)
	writer.Write(body)

	return nil
}

作者:tgulacs    项目:aosto   
func appendLink(tw *tar.Writer, tfh io.Seeker, fn string) (pos1 uint64, pos2 uint64, err error) {
	if !fileIsSymlink(fn) {
		return appendFile(tw, tfh, fn)
	}
	logger.Tracef("adding link %s (%s) to %s", tfh, fn, tw)
	hdr, e := FileTarHeader(fn)
	hdr.Size = 0
	hdr.Typeflag = tar.TypeSymlink
	hdr.Linkname = BaseName(FindLinkOrigin(fn, false))
	// logger.Printf("fn=%s hdr=%+v tm=%s", fn, hdr, hdr.Typeflag)
	if e != nil {
		err = e
		return
	}
	p, e := tfh.Seek(0, 1)
	if e != nil {
		err = e
		return
	}
	pos1 = uint64(p)
	if err = WriteTar(tw, hdr, nil); err != nil {
		return
	}
	_ = tw.Flush()
	if p, err = tfh.Seek(0, 1); err != nil {
		return
	}
	pos2 = uint64(p)
	return
}

作者:hyperledge    项目:fabri   
//WriteGopathSrc tars up files under gopath src
func WriteGopathSrc(tw *tar.Writer, excludeDir string) error {
	gopath := os.Getenv("GOPATH")
	// Only take the first element of GOPATH
	gopath = filepath.SplitList(gopath)[0]

	rootDirectory := filepath.Join(gopath, "src")
	vmLogger.Infof("rootDirectory = %s", rootDirectory)

	if err := WriteFolderToTarPackage(tw, rootDirectory, excludeDir, includeFileTypes, nil); err != nil {
		vmLogger.Errorf("Error writing folder to tar package %s", err)
		return err
	}

	// Add the certificates to tar
	if viper.GetBool("peer.tls.enabled") {
		err := WriteFileToPackage(viper.GetString("peer.tls.cert.file"), "src/certs/cert.pem", tw)
		if err != nil {
			return fmt.Errorf("Error writing cert file to package: %s", err)
		}
	}

	// Write the tar file out
	if err := tw.Close(); err != nil {
		return err
	}
	//ioutil.WriteFile("/tmp/chaincode_deployment.tar", inputbuf.Bytes(), 0644)
	return nil
}

作者:francoishil    项目:golang-web-dr   
func writeEndOfTarStreamHeader(tarWriter *tar.Writer) {
	hdr := &tar.Header{
		Name: END_OF_TAR_FILENAME,
	}
	err := tarWriter.WriteHeader(hdr)
	CheckError(err)
}

作者:rajkumargithu    项目:lattic   
func addFileToTar(fileWriter *os.File, tarWriter *tar.Writer, info os.FileInfo, containingPath string, fullPath string) error {
	var (
		relpath string
		err     error
	)

	if relpath, err = filepath.Rel(containingPath, fullPath); err != nil {
		return err
	}

	if relpath == fileWriter.Name() || relpath == "." || relpath == ".." {
		return nil
	}

	if h, _ := tar.FileInfoHeader(info, fullPath); h != nil {
		h.Name = relpath
		if err := tarWriter.WriteHeader(h); err != nil {
			return err
		}
	}

	if !info.IsDir() {
		fr, err := os.Open(fullPath)
		if err != nil {
			return err
		}
		defer fr.Close()
		if _, err := io.Copy(tarWriter, fr); err != nil {
			return err
		}
	}

	return nil
}

作者:Celluliodi    项目:flanne   
// copyFile copies src to tar file dst.
func copyFile(tw *tar.Writer, dst, src string) error {
	s, err := os.Open(src)
	if err != nil {
		return fmt.Errorf("unable to open %v: %v", src, err)
	}
	defer s.Close()
	fi, err := s.Stat()
	if err != nil {
		return fmt.Errorf("unable to stat %v: %v", src, err)
	}

	hdr, err := tar.FileInfoHeader(fi, dst)
	if err != nil {
		return fmt.Errorf("unable to create tar header for %v: %v", dst, err)
	}
	hdr.Name = dst
	if err := tw.WriteHeader(hdr); err != nil {
		return fmt.Errorf("unable to write header for %v: %v", dst, err)
	}
	_, err = io.Copy(tw, s)
	if err != nil {
		return fmt.Errorf("unable to copy %v to %v: %v", src, dst, err)
	}
	return nil
}

作者:jianqi    项目:bosh-softlayer-stemcell   
func addFileToTarWriter(filePath string, tarWriter *tar.Writer) error {
	file, err := os.Open(filePath)
	if err != nil {
		return errors.New(fmt.Sprintf("Could not open file '%s', got error '%s'", filePath, err.Error()))
	}
	defer file.Close()

	stat, err := file.Stat()
	if err != nil {
		return errors.New(fmt.Sprintf("Could not get stat for file '%s', got error '%s'", filePath, err.Error()))
	}

	header := &tar.Header{
		Name:    filePath,
		Size:    stat.Size(),
		Mode:    int64(stat.Mode()),
		ModTime: stat.ModTime(),
	}

	err = tarWriter.WriteHeader(header)
	if err != nil {
		return errors.New(fmt.Sprintf("Could not write header for file '%s', got error '%s'", filePath, err.Error()))
	}

	_, err = io.Copy(tarWriter, file)
	if err != nil {
		return errors.New(fmt.Sprintf("Could not copy the file '%s' data to the tarball, got error '%s'", filePath, err.Error()))
	}

	return nil
}

作者:Joinhac    项目:peon   
func targzWrite(tarWriter *tar.Writer, h *tar.Header, r io.Reader) {
	err := tarWriter.WriteHeader(h)
	panicOnError(err, "Failed to write tar entry header")

	_, err = io.Copy(tarWriter, r)
	panicOnError(err, "Failed to copy")
}

作者:tgulacs    项目:aosto   
// Writes the given file into tarfn
func writeItem(tarfn string, fn string) (pos uint64, err error) {
	pos = 0
	sfh, err := os.Open(fn)
	defer sfh.Close()
	if sfi, err := sfh.Stat(); err == nil {
		hdr := new(tar.Header)
		hdr.Name = sfi.Name()
		hdr.Size = sfi.Size()
		hdr.Mode = int64(sfi.Mode().Perm())
		hdr.ModTime = sfi.ModTime()
		var (
			tw *tar.Writer
			f  ReadWriteSeekCloser
		)
		if tw, f, pos, err = OpenForAppend(tarfn); err == nil {
			defer f.Close()
			defer tw.Close() //LIFO
			if err := tw.WriteHeader(hdr); err == nil {
				if zw, err := gzip.NewWriterLevel(tw, flate.BestCompression); err == nil {
					defer zw.Close()
					_, err = io.Copy(zw, sfh)
				}
			}
		}
	}
	return
}

作者:kte    项目:slu   
func targzWalk(dirPath string, tw *tar.Writer) {
	var walkfunc filepath.WalkFunc

	walkfunc = func(path string, fi os.FileInfo, err error) error {
		h, err := tar.FileInfoHeader(fi, "")
		handleError(err)

		h.Name = "./app/" + path

		if fi.Mode()&os.ModeSymlink != 0 {
			linkPath, err := os.Readlink(path)
			handleError(err)
			h.Linkname = linkPath
		}

		err = tw.WriteHeader(h)
		handleError(err)

		if fi.Mode()&os.ModeDir == 0 && fi.Mode()&os.ModeSymlink == 0 {
			fr, err := os.Open(path)
			handleError(err)
			defer fr.Close()

			_, err = io.Copy(tw, fr)
			handleError(err)
		}
		return nil
	}

	filepath.Walk(dirPath, walkfunc)
}

作者:eljefedelrodeodeljef    项目:kubernete   
func recursiveTar(base, file string, tw *tar.Writer) error {
	filepath := path.Join(base, file)
	stat, err := os.Stat(filepath)
	if err != nil {
		return err
	}
	if stat.IsDir() {
		files, err := ioutil.ReadDir(filepath)
		if err != nil {
			return err
		}
		for _, f := range files {
			if err := recursiveTar(base, path.Join(file, f.Name()), tw); err != nil {
				return err
			}
		}
		return nil
	}
	hdr, err := tar.FileInfoHeader(stat, filepath)
	if err != nil {
		return err
	}
	hdr.Name = file
	if err := tw.WriteHeader(hdr); err != nil {
		return err
	}
	f, err := os.Open(filepath)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = io.Copy(tw, f)
	return err
}

作者:tosha    项目:gob   
func (c *PackCmd) packSource(writer *tar.Writer, p *utils.Paths) error {
	base := path.Join(p.SrcDir, p.PkgName)
	files, err := ioutil.ReadDir(base)
	if err != nil {
		return err
	}

	for _, file := range files {
		if !file.IsDir() && strings.HasSuffix(file.Name(), ".go") {
			fpath := path.Join(base, file.Name())
			src, err := ioutil.ReadFile(fpath)
			if err != nil {
				return err
			}

			src, err = source.Prune(src)

			header := tar.Header{
				Name: fmt.Sprintf("src/%s/%s", p.PkgName, file.Name()),
				Mode: 0444,
				Size: int64(len(src)),
			}

			writer.WriteHeader(&header)
			writer.Write(src)
		}
	}

	return nil
}

作者:hyperledge    项目:fabri   
// WritePackage satisfies the platform interface for generating a docker package
// that encapsulates the environment for a CAR based chaincode
func (carPlatform *Platform) WritePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error {

	path, err := download(spec.ChaincodeID.Path)
	if err != nil {
		return err
	}

	var buf []string

	//let the executable's name be chaincode ID's name
	buf = append(buf, cutil.GetDockerfileFromConfig("chaincode.car.Dockerfile"))
	buf = append(buf, "COPY package.car /tmp/package.car")
	// invoking directly for maximum JRE compatiblity
	buf = append(buf, fmt.Sprintf("RUN java -jar /usr/local/bin/chaintool buildcar /tmp/package.car -o $GOPATH/bin/%s && rm /tmp/package.car", spec.ChaincodeID.Name))

	dockerFileContents := strings.Join(buf, "\n")
	dockerFileSize := int64(len([]byte(dockerFileContents)))

	//Make headers identical by using zero time
	var zeroTime time.Time
	tw.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize, ModTime: zeroTime, AccessTime: zeroTime, ChangeTime: zeroTime})
	tw.Write([]byte(dockerFileContents))

	err = cutil.WriteFileToPackage(path, "package.car", tw)
	if err != nil {
		return err
	}

	return nil
}

作者:wpjunio    项目:tsuru-clien   
func addFile(writer *tar.Writer, filepath string) error {
	f, err := os.Open(filepath)
	if err != nil {
		return err
	}
	defer f.Close()
	fi, err := os.Lstat(filepath)
	if err != nil {
		return err
	}
	if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
		target, err := os.Readlink(filepath)
		if err != nil {
			return err
		}
		return addSymlink(writer, filepath, target)
	}
	header, err := tar.FileInfoHeader(fi, "")
	if err != nil {
		return err
	}
	header.Name = filepath
	err = writer.WriteHeader(header)
	if err != nil {
		return err
	}
	n, err := io.Copy(writer, f)
	if err != nil {
		return err
	}
	if n != fi.Size() {
		return io.ErrShortWrite
	}
	return nil
}

作者:kkroenin    项目:repeat   
func saveWalk(srcBasePath string, tw *tar.Writer, filterset filter.FilterSet, bucket fshash.Bucket, hasherFactory func() hash.Hash) error {
	preVisit := func(filenode *fs.FilewalkNode) error {
		if filenode.Err != nil {
			return filenode.Err
		}
		hdr, file := fs.ScanFile(srcBasePath, filenode.Path, filenode.Info)
		// apply filters.  on scans, this is pretty easy, all of em just apply to the stream in memory.
		hdr = filterset.Apply(hdr)
		// flaten time to seconds.  this tar writer impl doesn't do subsecond precision.
		// the writer will flatten it internally of course, but we need to do it here as well
		// so that the hash and the serial form are describing the same thing.
		hdr.ModTime = hdr.ModTime.Truncate(time.Second)
		wat := tar.Header(hdr) // this line is... we're not gonna talk about this.
		tw.WriteHeader(&wat)
		if file == nil {
			bucket.Record(hdr, nil)
		} else {
			defer file.Close()
			hasher := hasherFactory()
			tee := io.MultiWriter(tw, hasher)
			_, err := io.Copy(tee, file)
			if err != nil {
				return err
			}
			bucket.Record(hdr, hasher.Sum(nil))
		}
		return nil
	}
	return fs.Walk(srcBasePath, preVisit, nil)
}

作者:wantedl    项目:ris   
func writeTarGz(filePath, baseDir string, tarGzWriter *tar.Writer, fileInfo os.FileInfo) error {
	file, err := os.Open(filePath)

	if err != nil {
		return err
	}
	defer file.Close()

	relativePath, err := filepath.Rel(baseDir, filePath)

	if err != nil {
		return err
	}

	header := new(tar.Header)
	header.Name = relativePath
	header.Size = fileInfo.Size()
	header.Mode = int64(fileInfo.Mode())
	header.ModTime = fileInfo.ModTime()

	err = tarGzWriter.WriteHeader(header)

	if err != nil {
		return err
	}

	_, err = io.Copy(tarGzWriter, file)

	if err != nil {
		return err
	}

	return nil
}

作者:oywc41    项目:MYP   
// 因为要在 defer 中关闭文件,所以要单独创建一个函数
func tarFile(srcBase, srcRelative string, tw *tar.Writer, fi os.FileInfo) (err error) {
	// 获取完整路径
	srcFull := srcBase + srcRelative

	// 写入文件信息
	hdr, er := tar.FileInfoHeader(fi, "")
	if er != nil {
		return er
	}
	hdr.Name = srcRelative

	if er = tw.WriteHeader(hdr); er != nil {
		return er
	}

	// 打开要打包的文件,准备读取
	fr, er := os.Open(srcFull)
	if er != nil {
		return er
	}
	defer fr.Close()

	// 将文件数据写入 tw 中
	if _, er = io.Copy(tw, fr); er != nil {
		return er
	}
	return nil
}

作者:Celluliodi    项目:flanne   
// synthesizeMain generates a new main func and writes it to the tarball.
func synthesizeMain(tw *tar.Writer, appFiles []string) error {
	appMap := make(map[string]bool)
	for _, f := range appFiles {
		appMap[f] = true
	}
	var f string
	for i := 0; i < 100; i++ {
		f = fmt.Sprintf("app_main%d.go", i)
		if !appMap[filepath.Join(*rootDir, f)] {
			break
		}
	}
	if appMap[filepath.Join(*rootDir, f)] {
		return fmt.Errorf("unable to find unique name for %v", f)
	}
	hdr := &tar.Header{
		Name: f,
		Mode: 0644,
		Size: int64(len(newMain)),
	}
	if err := tw.WriteHeader(hdr); err != nil {
		return fmt.Errorf("unable to write header for %v: %v", f, err)
	}
	if _, err := tw.Write([]byte(newMain)); err != nil {
		return fmt.Errorf("unable to write %v to tar file: %v", f, err)
	}
	return nil
}

作者:wangzhezh    项目:golangfiletoo   
//compress the file (if compress to the zip file , using the similar package: zip.FileInfoHeader)
func Filecompress(tw *tar.Writer, dir string, fi os.FileInfo) error {

	//打开文件 open当中是 目录名称/文件名称 构成的组合
	log.Println(dir + fi.Name())
	fr, err := os.Open(dir + fi.Name())
	log.Println(fr.Name())
	if err != nil {
		return err
	}
	defer fr.Close()

	hdr, err := tar.FileInfoHeader(fi, "")

	hdr.Name = fr.Name()
	if err = tw.WriteHeader(hdr); err != nil {
		return err
	}

	_, err = io.Copy(tw, fr)
	if err != nil {
		return err
	}
	//打印文件名称
	log.Println("add the file: " + fi.Name())
	return nil

}


问题


面经


文章

微信
公众号

扫码关注公众号