本文整理汇总了Golang中archive/tar.Writer类的典型用法代码### 示例。如果您正苦于以下问题:Golang Writer类的具体用法?Golang Writer怎么用?Golang Writer使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。

在下文中一共展示了Writer类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: writeFile

/*
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
}

开发者ID:vizidrix,项目名称:gotarpack,代码行数:32,代码来源:tarpack.go

示例2: packLib

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
}

开发者ID:toshaf,项目名称:gobl,代码行数:28,代码来源:pack.go

示例3: appendLink

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
}

开发者ID:tgulacsi,项目名称:aostor,代码行数:30,代码来源:compact_staging.go

示例4: WriteGopathSrc

//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
}

开发者ID:hyperledger,项目名称:fabric,代码行数:29,代码来源:writer.go

示例5: writeEndOfTarStreamHeader

func writeEndOfTarStreamHeader(tarWriter *tar.Writer) {
    hdr := &tar.Header{
        Name: END_OF_TAR_FILENAME,
    }
    err := tarWriter.WriteHeader(hdr)
    CheckError(err)
}

开发者ID:francoishill,项目名称:golang-web-dry,代码行数:7,代码来源:writeEndOfTarStreamHeader.go

示例6: addFileToTar

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
}

开发者ID:rajkumargithub,项目名称:lattice,代码行数:34,代码来源:droplet_runner_command_factory.go

示例7: copyFile

// 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
}

开发者ID:Celluliodio,项目名称:flannel,代码行数:26,代码来源:aebundler.go

示例8: addFileToTarWriter

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
}

开发者ID:jianqiu,项目名称:bosh-softlayer-stemcells,代码行数:31,代码来源:common.go

示例9: targzWrite

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")
}

开发者ID:Joinhack,项目名称:peony,代码行数:7,代码来源:pack.go

示例10: writeItem

// 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
}

开发者ID:tgulacsi,项目名称:aostor,代码行数:28,代码来源:tarhelper.go

示例11: targzWalk

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)
}

开发者ID:ktec,项目名称:slug,代码行数:31,代码来源:slug.go

示例12: recursiveTar

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
}

开发者ID:eljefedelrodeodeljefe,项目名称:kubernetes,代码行数:34,代码来源:cp.go

示例13: packSource

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
}

开发者ID:toshaf,项目名称:gobl,代码行数:30,代码来源:pack_source.go

示例14: WritePackage

// 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
}

开发者ID:hyperledger,项目名称:fabric,代码行数:32,代码来源:package.go

示例15: addFile

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
}

开发者ID:wpjunior,项目名称:tsuru-client,代码行数:35,代码来源:deploy.go

示例16: saveWalk

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)
}

开发者ID:kkroening,项目名称:repeatr,代码行数:30,代码来源:tar_stream.go

示例17: writeTarGz

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
}

开发者ID:wantedly,项目名称:risu,代码行数:34,代码来源:cache.go

示例18: tarFile

// 因为要在 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
}

开发者ID:oywc410,项目名称:MYPG,代码行数:29,代码来源:test.go

示例19: synthesizeMain

// 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
}

开发者ID:Celluliodio,项目名称:flannel,代码行数:29,代码来源:aebundler.go

示例20: Filecompress

//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

}

开发者ID:wangzhezhe,项目名称:golangfiletool,代码行数:28,代码来源:filetool.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng