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

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

示例1: StreamFile

// StreamFile streams a file or directory entry into StreamArchive.
func (s *StreamArchive) StreamFile(relPath string, fi os.FileInfo, data []byte) error {
    if fi.IsDir() {
        fh, err := tar.FileInfoHeader(fi, "")
        if err != nil {
            return err
        }
        fh.Name = relPath + "/"
        if err = s.Writer.WriteHeader(fh); err != nil {
            return err
        }
    } else {
        target := ""
        if fi.Mode()&os.ModeSymlink != 0 {
            target = string(data)
        }

        fh, err := tar.FileInfoHeader(fi, target)
        if err != nil {
            return err
        }
        fh.Name = filepath.Join(relPath, fi.Name())
        if err = s.Writer.WriteHeader(fh); err != nil {
            return err
        }

        if _, err = s.Writer.Write(data); err != nil {
            return err
        }
    }
    return nil
}

开发者ID:xuewuhen,项目名称:cae,代码行数:32,代码来源:stream.go

示例2: addFileToTar

func addFileToTar(zw *tar.Writer, item someutils.ArchiveItem, isVerbose bool, outPipe io.Writer) error {
    if isVerbose {
        fmt.Fprintf(outPipe, "Adding %s\n", item.FileSystemPath)
    }
    binfo, err := os.Stat(item.FileSystemPath)
    if err != nil {
        return err
    }
    if binfo.IsDir() {
        header, err := tar.FileInfoHeader(binfo, "")
        if err != nil {
            return err
        }
        header.Name = item.ArchivePath
        err = zw.WriteHeader(header)
        if err != nil {
            return err
        }
        file, err := os.Open(item.FileSystemPath)
        if err != nil {
            return err
        }
        fis, err := file.Readdir(0)
        for _, fi := range fis {
            err = addFileToTar(zw, someutils.ArchiveItem{filepath.Join(item.FileSystemPath, fi.Name()), filepath.Join(item.ArchivePath, fi.Name()), nil}, isVerbose, outPipe)
            if err != nil {
                return err
            }
        }
    } else {
        header, err := tar.FileInfoHeader(binfo, "")
        if err != nil {
            return err
        }
        header.Name = item.ArchivePath
        err = zw.WriteHeader(header)
        if err != nil {
            return err
        }
        bf, err := os.Open(item.FileSystemPath)
        if err != nil {
            return err
        }
        defer bf.Close()
        _, err = io.Copy(zw, bf)
        if err != nil {
            return err
        }
        err = zw.Flush()
        if err != nil {
            return err
        }
        err = bf.Close()
        if err != nil {
            return err
        }
    }
    return err
}

开发者ID:ando-masaki,项目名称:someutils,代码行数:59,代码来源:tar.go

示例3: addDir


func addDir(writer *tar.Writer, dirpath string) error {
    dir, err := os.Open(dirpath)
    if err != nil {
        return err
    }
    defer dir.Close()
    fi, err := dir.Stat()
    if err != nil {
        return err
    }
    header, err := tar.FileInfoHeader(fi, "")
    if err != nil {
        return err
    }
    header.Name = dirpath
    err = writer.WriteHeader(header)
    if err != nil {
        return err
    }
    fis, err := dir.Readdir(0)
    if err != nil {
        return err
    }
    for _, fi := range fis {
        if fi.IsDir() {
            err = addDir(writer, path.Join(dirpath, fi.Name()))
        } else {
            err = addFile(writer, path.Join(dirpath, fi.Name()))
        }
        if err != nil {
            return err
        }
    }
    return nil
}

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

示例4: writeFileToTarWriter

func writeFileToTarWriter(tarWriter *tar.Writer, info os.FileInfo, absoluteFilePath, overwriteFileName string, isOnlyFile bool) {
    hdr, err := tar.FileInfoHeader(info, "")
    CheckError(err)
    if overwriteFileName != "" {
        hdr.Name = overwriteFileName
    }

    if hdr.Xattrs == nil {
        hdr.Xattrs = map[string]string{}
    }
    hdr.Xattrs["SIZE"] = fmt.Sprintf("%d", info.Size())
    if isOnlyFile {
        hdr.Xattrs["SINGLE_FILE_ONLY"] = "1"
    }

    err = tarWriter.WriteHeader(hdr)
    CheckError(err)

    if !info.IsDir() {
        file, err := os.Open(absoluteFilePath)
        CheckError(err)
        defer file.Close()

        _, err = io.Copy(tarWriter, file)
        CheckError(err)
    }
}

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

示例5: createTarArchive

func createTarArchive(files []string, output io.WriteCloser) error {
    archive := tar.NewWriter(output)
    defer archive.Close()

    for _, path := range files {
        file, err := os.Open(path)
        if err != nil {
            return fmt.Errorf("Unable to read file %s: %s", path, err)
        }
        defer file.Close()

        fi, err := file.Stat()
        if err != nil {
            return fmt.Errorf("Unable to get fileinfo for %s: %s", path, err)
        }

        header, err := tar.FileInfoHeader(fi, path)
        if err != nil {
            return fmt.Errorf("Failed to create tar header for %s: %s", path, err)
        }

        if err := archive.WriteHeader(header); err != nil {
            return fmt.Errorf("Failed to write tar header for %s: %s", path, err)
        }

        if _, err := io.Copy(archive, file); err != nil {
            return fmt.Errorf("Failed to copy %s data to archive: %s", path, err)
        }
    }
    return nil
}

开发者ID:cewood,项目名称:packer,代码行数:31,代码来源:post-processor.go

示例6: compress

func (wft *tarWalk) compress(name, fpath string, fi os.FileInfo) (bool, error) {
    isSym := fi.Mode()&os.ModeSymlink > 0
    link := ""
    if isSym {
        link, _ = os.Readlink(fpath)
    }

    hdr, err := tar.FileInfoHeader(fi, link)
    if err != nil {
        return false, err
    }
    hdr.Name = name

    tw := wft.tw
    err = tw.WriteHeader(hdr)
    if err != nil {
        return false, err
    }

    if isSym == false {
        fr, err := os.Open(fpath)
        if err != nil {
            return false, err
        }
        defer fr.Close()
        _, err = io.Copy(tw, fr)
        if err != nil {
            return false, err
        }
        tw.Flush()
    }

    return true, nil
}

开发者ID:Linvas,项目名称:ant,代码行数:34,代码来源:pack.go

示例7: Walk

func (t *tarRenderer) Walk(path string, finfo os.FileInfo, err error) error {
    if err != nil {
        return err
    }

    fh, err := tar.FileInfoHeader(finfo, "")
    if err != nil {
        return err
    }

    fh.Name = "download/" + path
    err = t.w.WriteHeader(fh)
    if err != nil {
        return err
    }

    if finfo.IsDir() {
        return nil
    }

    f, err := os.Open(filepath.Join(t.root, path))
    if err != nil {
        return err
    }
    defer f.Close()

    _, err = io.Copy(t.w, f)
    return err
}

开发者ID:jonathanwei,项目名称:fproxy,代码行数:29,代码来源:tarrenderer.go

示例8: WriteToTar

func WriteToTar(tarWriter *tar.Writer, file *TarFile) error {
    hdr, err := tar.FileInfoHeader(file.Info, "")
    if err != nil {
        return fmt.Errorf("Unable to get tar FileInfoHeader of tar file '%s', error: %s", file.FileName, err.Error())
    }

    hdr.Name = file.FileName

    if hdr.Xattrs == nil {
        hdr.Xattrs = map[string]string{}
    }
    hdr.Xattrs["SIZE"] = fmt.Sprintf("%d", file.Info.Size())

    if file.IsOnlyFile {
        hdr.Xattrs["SINGLE_FILE_ONLY"] = "1"
    }

    err = tarWriter.WriteHeader(hdr)
    if err != nil {
        return fmt.Errorf("Unable to write tar header for file '%s', error: %s", file.FileName, err.Error())
    }

    if !file.HasContent() {
        return nil
    }

    defer file.ContentReadCloser.Close()
    _, err = io_throttler.CopyThrottled(io_throttler.DefaultIOThrottlingBandwidth, tarWriter, file.ContentReadCloser)
    return err
}

开发者ID:golang-devops,项目名称:go-psexec,代码行数:30,代码来源:write_to_tar.go

示例9: createTarArchive

func (ctx *runContext) createTarArchive(fd *os.File, dir string) error {
    tarFd := tar.NewWriter(fd)
    defer tarFd.Close()
    err := filepath.Walk(dir, func(name string, info os.FileInfo, err error) error {
        if err != nil {
            return errgo.Mask(err, errgo.Any)
        }
        if info.IsDir() {
            return nil
        }
        tarHeader, err := tar.FileInfoHeader(info, name)
        if err != nil {
            return fmt.Errorf("fail to build tar header: %v", err)
        }
        err = tarFd.WriteHeader(tarHeader)
        if err != nil {
            return fmt.Errorf("fail to write tar header: %v", err)
        }
        fileFd, err := os.OpenFile(name, os.O_RDONLY, 0600)
        if err != nil {
            return errgo.Mask(err, errgo.Any)
        }
        _, err = stdio.Copy(tarFd, fileFd)
        if err != nil {
            return errgo.Mask(err, errgo.Any)
        }
        return nil
    })
    if err != nil {
        return errgo.Mask(err, errgo.Any)
    }
    return nil
}

开发者ID:carriercomm,项目名称:cli-8,代码行数:33,代码来源:run.go

示例10: 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

示例11: tarcompress

func _tar_compress(call otto.FunctionCall) otto.Value {
    var (
        baseDir string
    )
    source, _ := call.Argument(0).ToString()
    target, _ := call.Argument(1).ToString()

    filename := filepath.Base(source)
    target = filepath.Join(target, fmt.Sprintf("%s.tar", filename))
    tarfile, err := os.Create(target)
    if err != nil {
        jsThrow(call, err)
    }
    defer tarfile.Close()

    tarball := tar.NewWriter(tarfile)
    defer tarball.Close()

    info, err := os.Stat(source)
    if err != nil {
        jsThrow(call, err)
    }

    if info.IsDir() {
        baseDir = filepath.Base(source)
    }

    err = filepath.Walk(source,
        func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }

            header, err := tar.FileInfoHeader(info, info.Name())
            if err != nil {
                return err
            }

            if baseDir != "" {
                header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
            }

            if err := tarball.WriteHeader(header); err != nil {
                return err
            }

            if info.IsDir() {
                return nil
            }

            file, err := os.Open(path)
            if err != nil {
                return err
            }
            defer file.Close()
            _, err = io.Copy(tarball, file)
            return err
        })
    return otto.Value{}
}

开发者ID:5Sigma,项目名称:Conduit,代码行数:60,代码来源:tar.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: 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

示例14: Tar

func Tar(base string, w io.Writer) error {
    tw := tar.NewWriter(w)
    err := filepath.Walk(base, func(path string, info os.FileInfo, err error) error {
        name, _ := filepath.Rel(base, path)
        if name == "." {
            return nil
        }

        hdr, err := tar.FileInfoHeader(info, "")
        if err != nil {
            return err
        }
        hdr.Name = name

        tw.WriteHeader(hdr)
        if info.IsDir() {
            return nil
        }

        // write file content
        f, err := os.Open(path)
        if err != nil {
            return err
        }
        _, err = io.Copy(tw, f)
        f.Close()
        return err
    })
    if err != nil {
        return err
    }
    return tw.Close()

}

开发者ID:icexin,项目名称:raftkv,代码行数:34,代码来源:tar.go

示例15: _getFileData

func _getFileData(fpath string) (*bytes.Buffer, error) {
    if data, err := ioutil.ReadFile(fpath); err != nil {
        return nil, err
    } else {
        if fi, err := os.Stat(fpath); err != nil {
            return nil, err
        } else {
            reads := bytes.NewReader(data)
            var buff bytes.Buffer
            gw := gzip.NewWriter(&buff)
            defer gw.Close()
            tw := tar.NewWriter(gw)
            defer tw.Close()
            hdr, err := tar.FileInfoHeader(fi, "")
            if err != nil {
                return nil, err
            }
            if err := tw.WriteHeader(hdr); err != nil {
                return nil, err
            }
            if _, err := io.Copy(tw, reads); err != nil {
                return nil, err
            }
            return &buff, nil
        }
    }
}

开发者ID:luffyhwl,项目名称:gopark,代码行数:27,代码来源:schedule_utils.go

示例16: addFile

//addFile adds the file named name as a tar entry named tarname
func addFile(name string, tarname string, tw *tar.Writer) error {
    if tarname == "" {
        tarname = name
    }
    fi, err := os.Stat(name)
    if err != nil {
        return err
    }
    hdr, err := tar.FileInfoHeader(fi, "")
    if err != nil {
        return err
    }
    hdr.Name = tarname

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

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

    _, err = io.Copy(tw, f)
    if err != nil {
        return err
    }
    tw.Flush()
    return nil
}

开发者ID:fun-alex-alex2006hw,项目名称:imageserver,代码行数:32,代码来源:generate_keys.go

示例17: addFile

func (i *dockerInput) addFile(path string, info os.FileInfo, err error) error {
    if err != nil {
        return err
    }
    th, err := tar.FileInfoHeader(info, path)
    if err != nil {
        return err
    }
    th.Name = path[1:] //filepath.Join(cpath, path)
    if si, ok := info.Sys().(*syscall.Stat_t); ok {
        th.Uid = int(si.Uid)
        th.Gid = int(si.Gid)
    }

    if err := i.tw.WriteHeader(th); err != nil {
        return err
    }

    if !info.Mode().IsDir() {
        file, err := os.Open(path)
        if err != nil {
            return err
        }
        if _, err := io.Copy(i.tw, file); err != nil {
            return err
        }
    }
    return nil
}

开发者ID:discordianfish,项目名称:byte-piper,代码行数:29,代码来源:docker_input.go

示例18: walkTarFunc

func walkTarFunc(tw *tar.Writer) filepath.WalkFunc {
    fn := func(path string, info os.FileInfo, err error) error {
        if info.Mode().IsDir() {
            return nil
        }
        f, err := os.Open(path)
        if err != nil {
            return err
        }
        defer f.Close()
        hdr, err := tar.FileInfoHeader(info, "")
        hdr.Name = path
        if err != nil {
            return err
        }
        err = tw.WriteHeader(hdr)
        if err != nil {
            return err
        }
        _, err = io.Copy(tw, f)
        if err != nil {
            return err
        }
        return nil
    }
    return fn
}

开发者ID:kelseyhightower,项目名称:targz,代码行数:27,代码来源:targz.go

示例19: 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

示例20: 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

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