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

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

示例1: getTarFileBytes

func getTarFileBytes(file *os.File, path string) ([]byte, error) {
    _, err := file.Seek(0, 0)
    if err != nil {
        fmt.Errorf("error seeking file: %v", err)
    }

    var fileBytes []byte
    fileWalker := func(t *tarball.TarFile) error {
        if filepath.Clean(t.Name()) == path {
            fileBytes, err = ioutil.ReadAll(t.TarStream)
            if err != nil {
                return err
            }
        }

        return nil
    }

    tr := tar.NewReader(file)
    if err := tarball.Walk(*tr, fileWalker); err != nil {
        return nil, err
    }

    if fileBytes == nil {
        return nil, fmt.Errorf("file %q not found", path)
    }

    return fileBytes, nil
}

开发者ID:rtrentin73,项目名称:docker2aci,代码行数:29,代码来源:file.go

示例2: readLayer

func readLayer(r io.ReadSeeker, id string) (json, version []byte, layer *tar.Reader, err error) {
    if _, err = r.Seek(0, 0); err != nil {
        return
    }

    tr := tar.NewReader(r)
    for {
        var h *tar.Header
        h, err = tr.Next()
        if err == io.EOF {
            return nil, nil, nil, fmt.Errorf("layer not found")
        }
        if err != nil {
            return
        }
        if h.Typeflag != tar.TypeDir || h.Name != id+"/" {
            continue
        }
        if version, err = readFile(tr, id+"/VERSION"); err != nil {
            return
        }
        if json, err = readFile(tr, id+"/json"); err != nil {
            return
        }
        var r io.Reader
        r, err = nextFile(tr, id+"/layer.tar")
        if err != nil {
            return
        }
        layer = tar.NewReader(r)
        return
    }
}

开发者ID:ericchiang,项目名称:stomper,代码行数:33,代码来源:stomper.go

示例3: UpdatePixel

func (pa *PixelApi) UpdatePixel(w http.ResponseWriter, r *http.Request) {
    defer r.Body.Close()
    pixel := w.(httptools.VarsResponseWriter).Vars()["pixel"].(*Pixel)

    buf := &bytes.Buffer{}
    io.Copy(buf, r.Body)
    if buf.Len() <= 0 {
        http.Error(w, "Empty fs", http.StatusBadRequest)
        return
    }

    ctr, err := pa.cc.CreateContainer(tar.NewReader(bytes.NewReader(buf.Bytes())), nil)
    if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    StopContainer(pixel.Container)
    pixel.Container = ctr
    pixel.Filesystem = fsObject(tar.NewReader(bytes.NewReader(buf.Bytes())))
    pixel.LastImage = &bytes.Buffer{}
    pixel.Broken = false
    io.Copy(pixel.LastImage, bytes.NewReader(blackPixel.Bytes()))

    pa.Messages <- &Message{
        Pixel: pixel.Id,
        Type:  TypeChange,
    }

    go pa.pixelListener(pixel)

    http.Error(w, pixel.Id, http.StatusCreated)
}

开发者ID:voxelbrain,项目名称:pixelpixel,代码行数:33,代码来源:pixelapi.go

示例4: DetectArchiveCompression

// DetectArchiveCompression takes a source reader and will determine the
// compression type to use, if any. It will return a *tar.Reader that can be
// used to read the archive.
func DetectArchiveCompression(r io.Reader) (*tar.Reader, error) {
    var comp Decompressor

    // setup a buffered reader
    br := bufio.NewReader(r)

    // loop over the registered decompressors to find the right one
    for _, c := range decompressorTypes {
        if c.Detect(br) {
            comp = c
            break
        }
    }

    // Create the reader if a compression handler was found, else fall back on
    // using no compression.
    if comp != nil {
        // Create the reader
        arch, err := comp.NewReader(br)
        if err != nil {
            return nil, err
        }
        defer func() {
            if cl, ok := arch.(io.ReadCloser); ok {
                cl.Close()
            }
        }()
        return tar.NewReader(arch), nil
    }

    return tar.NewReader(br), nil
}

开发者ID:wallyqs,项目名称:util,代码行数:35,代码来源:utils.go

示例5: TarFileList

// TarFileList ...
func TarFileList(filename string) ([]string, error) {
    reader, err := os.Open(filename)
    if err != nil {
        return nil, err
    }
    defer reader.Close()
    var tarReader *tar.Reader
    if strings.HasSuffix(filename, ".gz") ||
        strings.HasSuffix(filename, ".tgz") {
        gzipReader, err := gzip.NewReader(reader)
        if err != nil {
            return nil, err
        }
        tarReader = tar.NewReader(gzipReader)
    } else {
        tarReader = tar.NewReader(reader)
    }
    var files []string
    for {
        header, err := tarReader.Next()
        if err != nil {
            if err == io.EOF {
                break
            }
            return files, err
        }
        if header == nil {
            break
        }
        files = append(files, header.Name)
    }
    return files, nil
}

开发者ID:jvillasante,项目名称:goeg,代码行数:34,代码来源:main.go

示例6: Load

func (t *TarInfo) Load(file io.ReadSeeker) {
    var reader *tar.Reader
    file.Seek(0, 0)
    gzipReader, err := gzip.NewReader(file)
    if err != nil {
        // likely not a gzip compressed file
        file.Seek(0, 0)
        reader = tar.NewReader(file)
    } else {
        reader = tar.NewReader(gzipReader)
    }
    for {
        header, err := reader.Next()
        if err == io.EOF {
            // end of tar file
            break
        } else if err != nil {
            // error occured
            logger.Debug("[TarInfoLoad] Error when reading tar stream tarsum. Disabling TarSum, TarFilesInfo. Error: %s", err.Error())
            t.Error = TarError(err.Error())
            return
        }
        t.TarSum.Append(header, reader)
        t.TarFilesInfo.Append(header)
    }
}

开发者ID:hqm,项目名称:go-docker-registry,代码行数:26,代码来源:tar.go

示例7: tarFilesCount

func tarFilesCount(sourcefile string) int {
    flreader, _ := os.Open(sourcefile)
    defer flreader.Close()
    var fltarReader *tar.Reader
    var flReader io.ReadCloser = flreader

    if strings.HasSuffix(sourcefile, ".gz") ||
        strings.HasSuffix(sourcefile, ".tgz") {
        flgzipReader, err := gzip.NewReader(flreader)
        checkErr("In tarFilesCounter - NewReader", err)
        fltarReader = tar.NewReader(flgzipReader)
        defer flReader.Close()
    } else if strings.HasSuffix(sourcefile, ".bz2") {
        flbz2Reader := bzip2.NewReader(flreader)
        fltarReader = tar.NewReader(flbz2Reader)
    } else {
        fltarReader = tar.NewReader(flreader)
    }

    trfl := fltarReader
    counter := 0
    for {
        _, err := trfl.Next()
        if err != nil {
            if err == io.EOF {
                break
            }
            checkErr("Extract error::ReadTarArchive", err)
        }
        counter++
    }
    fmt.Println("Files in archive -", counter)
    return counter
}

开发者ID:bvp,项目名称:gtvm,代码行数:34,代码来源:extract.go

示例8: getTarReader

// getTarReader returns a TarReaderCloser associated with the specified io.Reader.
//
// Gzip/Bzip2/XZ detection is done by using the magic numbers:
// Gzip: the first two bytes should be 0x1f and 0x8b. Defined in the RFC1952.
// Bzip2: the first three bytes should be 0x42, 0x5a and 0x68. No RFC.
// XZ: the first three bytes should be 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00. No RFC.
func getTarReader(r io.Reader) (*TarReadCloser, error) {
    br := bufio.NewReader(r)
    header, err := br.Peek(readLen)
    if err == nil {
        switch {
        case bytes.HasPrefix(header, gzipHeader):
            gr, err := gzip.NewReader(br)
            if err != nil {
                return nil, err
            }
            return &TarReadCloser{tar.NewReader(gr), gr}, nil
        case bytes.HasPrefix(header, bzip2Header):
            bzip2r := ioutil.NopCloser(bzip2.NewReader(br))
            return &TarReadCloser{tar.NewReader(bzip2r), bzip2r}, nil
        case bytes.HasPrefix(header, xzHeader):
            xzr, err := NewXzReader(br)
            if err != nil {
                return nil, err
            }
            return &TarReadCloser{tar.NewReader(xzr), xzr}, nil
        }
    }

    dr := ioutil.NopCloser(br)
    return &TarReadCloser{tar.NewReader(dr), dr}, nil
}

开发者ID:robinjha,项目名称:clair,代码行数:32,代码来源:tar.go

示例9: extractBz2

func extractBz2(body []byte, location string) (string, error) {
    bodyCopy := make([]byte, len(body))
    copy(bodyCopy, body)
    tarFile := bzip2.NewReader(bytes.NewReader(body))
    tarReader := tar.NewReader(tarFile)

    var dirList []string

    for {
        header, err := tarReader.Next()
        if err == io.EOF {
            break
        }
        dirList = append(dirList, header.Name)
    }

    basedir := findBaseDir(dirList)

    tarFile = bzip2.NewReader(bytes.NewReader(bodyCopy))
    tarReader = tar.NewReader(tarFile)

    for {
        header, err := tarReader.Next()
        if err == io.EOF {
            break
        } else if err != nil {
            //return location, err
        }

        path := filepath.Join(location, strings.Replace(header.Name, basedir, "", -1))
        info := header.FileInfo()

        if info.IsDir() {
            if err = os.MkdirAll(path, info.Mode()); err != nil {
                return location, err
            }
            continue
        }

        if header.Typeflag == tar.TypeSymlink {
            err = os.Symlink(header.Linkname, path)
            continue
        }

        file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
        if err == nil {
            defer file.Close()
        }
        _, err = io.Copy(file, tarReader)
        if err != nil {
            //return location, err
        }
    }
    return location, nil
}

开发者ID:arduino,项目名称:arduino-create-agent,代码行数:55,代码来源:download.go

示例10: UnTarGz

// Ungzip and untar from source file to destination directory
// you need check file exist before you call this function
func UnTarGz(srcFilePath string, destDirPath string) error {
    //fmt.Println("UnTarGzing " + srcFilePath + "...")
    // Create destination directory
    os.Mkdir(destDirPath, os.ModePerm)
    var tr *tar.Reader
    fr, err := os.Open(srcFilePath)
    if err != nil {
        return err
    }
    defer fr.Close()

    if strings.HasSuffix(srcFilePath, ".bz2") {
        br := bzip2.NewReader(fr)
        tr = tar.NewReader(br)
    } else {
        // Gzip reader
        gr, err := gzip.NewReader(fr)
        if err != nil {
            return err
        }
        defer gr.Close()
        // Tar reader
        tr = tar.NewReader(gr)
    }

    for {
        hdr, err := tr.Next()
        if err == io.EOF {
            // End of tar archive
            break
        }
        //handleError(err)
        //fmt.Println("UnTarGzing file..." + hdr.Name)
        // Check if it is diretory or file
        if hdr.Typeflag != tar.TypeDir {
            // Get files from archive
            // Create diretory before create file
            os.MkdirAll(destDirPath+"/"+path.Dir(hdr.Name), os.ModePerm)
            // Write data to file
            fw, _ := os.Create(destDirPath + "/" + hdr.Name)

            os.Chmod(destDirPath+"/"+hdr.Name, os.FileMode(hdr.Mode))

            if err != nil {
                return err
            }
            _, err = io.Copy(fw, tr)
            if err != nil {
                return err
            }
        }
    }
    //fmt.Println("Well done!")
    return nil
}

开发者ID:qingxialanshan,项目名称:InstallUtil,代码行数:57,代码来源:decompress.go

示例11: Extract

// Extract unpacks the tar reader that was passed into New(). This is
// broken out from new to give the caller time to set various
// settings in the Untar object.
func (u *Untar) Extract() error {
    // check for detect mode before the main setup, we'll change compression
    // to the intended type and setup a new reader to re-read the header
    switch u.Compression {
    case NONE:
        u.archive = tar.NewReader(u.source)

    case DETECT:
        arch, err := DetectArchiveCompression(u.source)
        if err != nil {
            return err
        }
        u.archive = arch

    default:
        // Look up the compression handler
        comp, exists := decompressorTypes[string(u.Compression)]
        if !exists {
            return fmt.Errorf("unrecognized decompression type %q", u.Compression)
        }

        // Create the reader
        arch, err := comp.NewReader(u.source)
        if err != nil {
            return err
        }
        defer func() {
            if cl, ok := arch.(io.ReadCloser); ok {
                cl.Close()
            }
        }()
        u.archive = tar.NewReader(arch)
    }

    for {
        header, err := u.archive.Next()
        if err == io.EOF {
            // EOF, ok, break to return
            break
        }
        if err != nil {
            // See note on logging above.
            return err
        }

        err = u.processEntry(header)
        if err != nil {
            // See note on logging above.
            return err
        }
    }

    return nil
}

开发者ID:wallyqs,项目名称:util,代码行数:57,代码来源:untar.go

示例12: TestPortAssignment

func TestPortAssignment(t *testing.T) {
    buf := makeFs(map[string]interface{}{
        "main.go": `package main

        import (
            "fmt"
            "os"
        )

        func main() {
            fmt.Printf("127.0.0.1:%s", os.Getenv("PORT"))
        }`,
    })
    fs := tar.NewReader(bytes.NewReader(buf))

    ctr1, err := lcc.CreateContainer(fs, nil)
    if err != nil {
        t.Fatalf("Could not start container: %s", err)
    }
    go ctr1.Cleanup()

    timer := time.AfterFunc(1*time.Second, func() {
        t.Fatalf("Timeout occured")
    })
    ctr1.Wait()
    timer.Stop()

    fs = tar.NewReader(bytes.NewReader(buf))
    ctr2, err := lcc.CreateContainer(fs, nil)
    if err != nil {
        t.Fatalf("Could not start container: %s", err)
    }
    go ctr2.Cleanup()

    timer = time.AfterFunc(1*time.Second, func() {
        t.Fatalf("Timeout occured")
    })
    ctr2.Wait()
    timer.Stop()

    if ctr1.Logs() != ctr1.Address().String() {
        t.Fatalf("Specified and injected ports differ. Injected %s, got %s", ctr1.Address(), ctr1.Logs())
    }
    if ctr2.Logs() != ctr2.Address().String() {
        t.Fatalf("Specified and injected ports differ. Injected %s, got %s", ctr2.Address(), ctr2.Logs())
    }
    if ctr1.Logs() == ctr2.Logs() {
        t.Fatalf("Same port was assigned")
    }
}

开发者ID:voxelbrain,项目名称:pixelpixel,代码行数:50,代码来源:localcontainer_test.go

示例13: getTarReader

// getTarReader returns a tar.Reader associated with the specified io.Reader,
// optionally backed by a gzip.Reader if gzip compression is detected.
//
// Gzip detection is done by using the magic numbers defined in the RFC1952 :
// the first two bytes should be 0x1f and 0x8b..
func getTarReader(r io.Reader) (*tar.Reader, error) {
    br := bufio.NewReader(r)
    header, err := br.Peek(2)

    if err == nil && bytes.Equal(header, gzipHeader) {
        gr, err := gzip.NewReader(br)
        if err != nil {
            return nil, err
        }
        return tar.NewReader(gr), nil
    }

    return tar.NewReader(br), nil
}
开发者ID:kinvolk,项目名称:clair,代码行数:19,代码来源:tar.go


示例14: ExampleClient_CopyFromContainer
​ 点赞 1 ​
func ExampleClient_CopyFromContainer() {
    client, err := docker.NewClient("http://localhost:4243")
    if err != nil {
        log.Fatal(err)
    }
    cid := "a84849"
    var buf bytes.Buffer
    filename := "/tmp/output.txt"
    err = client.CopyFromContainer(docker.CopyFromContainerOptions{
        Container:    cid,
        Resource:     filename,
        OutputStream: &buf,
    })
    if err != nil {
        log.Fatalf("Error while copying from %s: %s\n", cid, err)
    }
    content := new(bytes.Buffer)
    r := bytes.NewReader(buf.Bytes())
    tr := tar.NewReader(r)
    tr.Next()
    if err != nil && err != io.EOF {
        log.Fatal(err)
    }
    if _, err := io.Copy(content, tr); err != nil {
        log.Fatal(err)
    }
    log.Println(buf.String())
}
开发者ID:CodeJuan,项目名称:deis,代码行数:28,代码来源:example_test.go


示例15: fetchCamliSrc
​ 点赞 1 ​
func fetchCamliSrc() {
    check(os.MkdirAll("/gopath/src/camlistore.org", 0777))
    check(os.Chdir("/gopath/src/camlistore.org"))

    res, err := http.Get("https://camlistore.googlesource.com/camlistore/+archive/" + *rev + ".tar.gz")
    check(err)
    defer res.Body.Close()
    gz, err := gzip.NewReader(res.Body)
    check(err)
    defer gz.Close()
    tr := tar.NewReader(gz)
    for {
        h, err := tr.Next()
        if err == io.EOF {
            break
        }
        check(err)
        if h.Typeflag == tar.TypeDir {
            check(os.MkdirAll(h.Name, os.FileMode(h.Mode)))
            continue
        }
        f, err := os.Create(h.Name)
        check(err)
        n, err := io.Copy(f, tr)
        if err != nil && err != io.EOF {
            log.Fatal(err)
        }
        if n != h.Size {
            log.Fatalf("Error when creating %v: wanted %v bytes, got %v bytes", h.Name, h.Size, n)
        }
        check(f.Close())
    }
}

开发者ID:camarox53,项目名称:coreos-baremetal,代码行数:33,代码来源:build-binaries.go

示例16: testArchiveStr

func testArchiveStr(t *testing.T, path string) []string {
    f, err := os.Open(path)
    if err != nil {
        t.Fatalf("err: %s", err)
    }
    defer f.Close()

    // Ungzip
    gzipR, err := gzip.NewReader(f)
    if err != nil {
        t.Fatalf("err: %s", err)
    }

    // Accumulator
    result := make([]string, 0, 10)

    // Untar
    tarR := tar.NewReader(gzipR)
    for {
        header, err := tarR.Next()
        if err == io.EOF {
            break
        }
        if err != nil {
            t.Fatalf("err: %s", err)
        }

        result = append(result, header.Name)
    }

    sort.Strings(result)
    return result
}

开发者ID:jrperritt,项目名称:terraform,代码行数:33,代码来源:push_test.go

示例17: TestContainerApiCopy

func (s *DockerSuite) TestContainerApiCopy(c *check.C) {
    testRequires(c, DaemonIsLinux)
    name := "test-container-api-copy"
    dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test.txt")

    postData := types.CopyConfig{
        Resource: "/test.txt",
    }

    status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
    c.Assert(err, check.IsNil)
    c.Assert(status, check.Equals, http.StatusOK)

    found := false
    for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
        h, err := tarReader.Next()
        if err != nil {
            if err == io.EOF {
                break
            }
            c.Fatal(err)
        }
        if h.Name == "test.txt" {
            found = true
            break
        }
    }
    c.Assert(found, check.Equals, true)
}

开发者ID:previousnext,项目名称:kube-ingress,代码行数:29,代码来源:docker_api_containers_test.go

示例18: ImportTar

func ImportTar(r io.Reader, ds dag.DAGService) (*dag.Node, error) {
    rall, err := ioutil.ReadAll(r)
    if err != nil {
        return nil, err
    }

    r = bytes.NewReader(rall)

    tr := tar.NewReader(r)

    root := new(dag.Node)
    root.Data = []byte("ipfs/tar")

    e := dagutil.NewDagEditor(root, ds)

    for {
        h, err := tr.Next()
        if err != nil {
            if err == io.EOF {
                break
            }
            return nil, err
        }

        header := new(dag.Node)

        headerBytes, err := marshalHeader(h)
        if err != nil {
            return nil, err
        }

        header.Data = headerBytes

        if h.Size > 0 {
            spl := chunk.NewRabin(tr, uint64(chunk.DefaultBlockSize))
            nd, err := importer.BuildDagFromReader(ds, spl)
            if err != nil {
                return nil, err
            }

            err = header.AddNodeLinkClean("data", nd)
            if err != nil {
                return nil, err
            }
        }

        _, err = ds.Add(header)
        if err != nil {
            return nil, err
        }

        path := escapePath(h.Name)
        err = e.InsertNodeAtPath(context.Background(), path, header, func() *dag.Node { return new(dag.Node) })
        if err != nil {
            return nil, err
        }
    }

    return e.Finalize(ds)
}

开发者ID:musha68k,项目名称:go-ipfs,代码行数:60,代码来源:format.go

示例19: main

func main() {
    var file *os.File
    var err error

    if file, err = os.Open("files/sample.tar.bz2"); err != nil {
        log.Fatalln(err)
    }
    defer file.Close()

    reader := tar.NewReader(bzip2.NewReader(file))

    var header *tar.Header
    for {
        header, err = reader.Next()
        if err == io.EOF {
            // ファイルの最後
            break
        }
        if err != nil {
            log.Fatalln(err)
        }

        buf := new(bytes.Buffer)
        if _, err = io.Copy(buf, reader); err != nil {
            log.Fatalln(err)
        }

        if err = ioutil.WriteFile("output/"+header.Name, buf.Bytes(), 0755); err != nil {
            log.Fatal(err)
        }
    }
}

开发者ID:qt-luigi,项目名称:golangcafe,代码行数:32,代码来源:tarbzip.go

示例20: Open

func (t *TapeArchive) Open(name string) (io.ReadCloser, int64, error) {
    f, err := os.Open(t.path)
    if err != nil {
        return nil, 0, err
    }

    r := tar.NewReader(f)

    for {
        h, err := r.Next()
        if err == io.EOF {
            break
        }
        if err != nil {
            return nil, 0, err
        }

        matched, err := filepath.Match(name, path.Base(h.Name))
        if err != nil {
            return nil, 0, err
        }

        if matched {
            return &TapeArchiveEntry{r, f}, h.Size, nil
        }
    }

    _ = f.Close()

    return nil, 0, os.ErrNotExist
}

开发者ID:jak-atx,项目名称:vic,代码行数:31,代码来源:archive.go

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