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

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

示例1: zipOpen

// zipOpen returns a zipReadCloser.
func (b *Bundle) zipOpen() (*zipReadCloser, error) {
    // If we don't have a Path, try to use the original ReaderAt.
    if b.Path == "" {
        r, err := zip.NewReader(b.r, b.size)
        if err != nil {
            return nil, err
        }
        return &zipReadCloser{Closer: ioutil.NopCloser(nil), Reader: r}, nil
    }
    f, err := os.Open(b.Path)
    if err != nil {
        return nil, err
    }
    fi, err := f.Stat()
    if err != nil {
        f.Close()
        return nil, err
    }
    r, err := zip.NewReader(f, fi.Size())
    if err != nil {
        f.Close()
        return nil, err
    }
    return &zipReadCloser{Closer: f, Reader: r}, nil
}

开发者ID:jameinel,项目名称:core,代码行数:26,代码来源:bundle.go

示例2: zipExeReaderElf

// zipExeReaderElf treats the file as a ELF binary
// (linux/BSD/etc... executable) and attempts to find a zip archive.
func zipExeReaderElf(rda io.ReaderAt, size int64) (*zip.Reader, error) {
    file, err := elf.NewFile(rda)
    if err != nil {
        return nil, err
    }

    var max int64
    for _, sect := range file.Sections {
        if sect.Type == elf.SHT_NOBITS {
            continue
        }

        // Check if this section has a zip file
        if zfile, err := zip.NewReader(sect, int64(sect.Size)); err == nil {
            return zfile, nil
        }

        // Otherwise move end of file pointer
        end := int64(sect.Offset + sect.Size)
        if end > max {
            max = end
        }
    }

    // No zip file within binary, try appended to end
    section := io.NewSectionReader(rda, max, size-max)
    return zip.NewReader(section, section.Size())
}

开发者ID:Skarlso,项目名称:slick,代码行数:30,代码来源:zipexe.go

示例3: ProcessDiff

func ProcessDiff(fname string, data1, data2 []byte, brief bool, skips string) (DiffResult, error) {
    diffResult := DiffResult{}
    diffResult.Title = fmt.Sprintf("ファイル %s は異なります", fname)
    if brief {
        return diffResult, nil
    }
    if isSkiping(fname, skips) {
        return diffResult, nil
    }
    if strings.HasSuffix(fname, "jar") || strings.HasSuffix(fname, "war") || strings.HasSuffix(fname, "zip") {
        diffResult.IsZip = true
        zipa, err := zip.NewReader(bytes.NewReader(data1), int64(len(data1)))
        if err != nil {
            return diffResult, err
        }
        zipb, err := zip.NewReader(bytes.NewReader(data2), int64(len(data2)))
        if err != nil {
            return diffResult, err
        }
        var buf bytes.Buffer
        DisplayAsText(&buf, ZipDiffContent(zipa, zipb, brief, skips))
        diffResult.Diff = buf.String()
    } else if strings.HasSuffix(fname, "class") {
        diffResult.Diff = godiff(string(jadfile(data1)), string(jadfile(data2)))
    } else if checkBinary(data1) {
        diffResult.IsBinary = true
    } else {
        diffResult.Diff = godiff(string(data1), string(data2))
    }
    return diffResult, nil
}

开发者ID:anykao,项目名称:p,代码行数:31,代码来源:diffjar.go

示例4: zipExeReaderMacho

// zipExeReaderMacho treats the file as a Mach-O binary
// (Mac OS X / Darwin executable) and attempts to find a zip archive.
func zipExeReaderMacho(rda io.ReaderAt, size int64) (*zip.Reader, error) {
    file, err := macho.NewFile(rda)
    if err != nil {
        return nil, err
    }

    var max int64
    for _, load := range file.Loads {
        seg, ok := load.(*macho.Segment)
        if ok {
            // Check if the segment contains a zip file
            if zfile, err := zip.NewReader(seg, int64(seg.Filesz)); err == nil {
                return zfile, nil
            }

            // Otherwise move end of file pointer
            end := int64(seg.Offset + seg.Filesz)
            if end > max {
                max = end
            }
        }
    }

    // No zip file within binary, try appended to end
    section := io.NewSectionReader(rda, max, size-max)
    return zip.NewReader(section, section.Size())
}

开发者ID:Skarlso,项目名称:slick,代码行数:29,代码来源:zipexe.go

示例5: fetchZip

func fetchZip(zipurl string) (*zip.Reader, error) {
    req, err := http.NewRequest("GET", zipurl, nil)
    if err != nil {
        return nil, err
    }

    req.Header.Set("User-Agent", userAgent)

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    if resp.StatusCode >= 400 {
        b, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            return nil, errors.New(resp.Status)
        }
        return nil, errors.New(string(b))
    }

    buf := &bytes.Buffer{}

    _, err = io.Copy(buf, resp.Body)
    if err != nil {
        return nil, err
    }

    b := bytes.NewReader(buf.Bytes())
    return zip.NewReader(b, int64(b.Len()))
}

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

示例6: ReadZip

func (hra *HttpReaderAt) ReadZip() error {
    reader, err := zip.NewReader(hra, hra.ContentLength)
    if err != nil {
        return err
    }
    for _, zf := range reader.File {
        if hra.Verbose {
            fmt.Printf("%s\n", zf.Name)
        }
        err = CreateParents(zf.Name)
        if err != nil {
            fmt.Println(err)
            continue
        }
        f, err := os.Create(zf.Name)
        if err != nil {
            fmt.Println(err)
            continue
        }
        rc, err := zf.Open()
        if err != nil {
            fmt.Println(err)
            continue
        }
        go WriteAllAndClose(rc, f)
    }
    return nil
}

开发者ID:hobinjk,项目名称:go-getter,代码行数:28,代码来源:get.go

示例7: WalkAny

// WalkAny tries to retrieve container information on containerPath. It supports:
// the empty container (/dev/null), local directories, zip archives
func WalkAny(containerPath string, filter FilterFunc) (*Container, error) {
    // empty container case
    if containerPath == NullPath {
        return &Container{}, nil
    }

    file, err := eos.Open(containerPath)
    if err != nil {
        return nil, errors.Wrap(err, 1)
    }

    defer file.Close()

    stat, err := file.Stat()
    if err != nil {
        return nil, errors.Wrap(err, 1)
    }

    if stat.IsDir() {
        if err != nil {
            return nil, errors.Wrap(err, 1)
        }

        // local directory case
        return WalkDir(containerPath, filter)
    }

    // zip archive case
    zr, err := zip.NewReader(file, stat.Size())
    if err != nil {
        return nil, errors.Wrap(err, 1)
    }

    return WalkZip(zr, filter)
}

开发者ID:itchio,项目名称:wharf,代码行数:37,代码来源:walk.go

示例8: TestGetArchiveIntegrationWhenInvalidFormat

func (s *S) TestGetArchiveIntegrationWhenInvalidFormat(c *gocheck.C) {
    expected := make(map[string]string)
    expected["gandalf-test-repo-master/README"] = "much WOW"
    oldBare := bare
    bare = "/tmp"
    repo := "gandalf-test-repo"
    file := "README"
    content := "much WOW"
    cleanUp, errCreate := CreateTestRepository(bare, repo, file, content)
    defer func() {
        cleanUp()
        bare = oldBare
    }()
    c.Assert(errCreate, gocheck.IsNil)
    zipContents, err := GetArchive(repo, "master", 99)
    reader := bytes.NewReader(zipContents)
    zipReader, err := zip.NewReader(reader, int64(len(zipContents)))
    c.Assert(err, gocheck.IsNil)
    for _, f := range zipReader.File {
        //fmt.Printf("Contents of %s:\n", f.Name)
        rc, err := f.Open()
        c.Assert(err, gocheck.IsNil)
        defer rc.Close()
        contents, err := ioutil.ReadAll(rc)
        c.Assert(err, gocheck.IsNil)
        c.Assert(string(contents), gocheck.Equals, expected[f.Name])
    }
}

开发者ID:kenpu,项目名称:gandalf,代码行数:28,代码来源:repository_test.go

示例9: installZip

func installZip(source *bytes.Reader, dest string) error {
    zr, err := zip.NewReader(source, int64(source.Len()))
    if err != nil {
        return err
    }

    for _, f := range zr.File {
        fileCopy, err := os.OpenFile(dest, installFlag, f.Mode())
        if err != nil {
            return err
        }
        defer fileCopy.Close()

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

        _, err = io.Copy(fileCopy, rc)
        if err != nil {
            return err
        }
    }

    return nil
}

开发者ID:Lin4ipsum,项目名称:cli,代码行数:27,代码来源:release_utils.go

示例10: extractFpUtil

func extractFpUtil(archive []byte) {
    zipReader, err := zip.NewReader(bytes.NewReader(archive), int64(len(archive)))
    if err != nil {
        utils.Log(utils.ERROR, "recognizer.extractFpUtil: failed to unzip fingerprint util archive: %v", err)
        return
    }

    for _, file := range zipReader.File {
        if len(file.Name) < len(fpUtil()) || file.Name[len(file.Name)-len(fpUtil()):] != fpUtil() {
            continue
        }

        src, err := file.Open()
        if err != nil {
            utils.Log(utils.ERROR, "recognizer.extractFpUtil: failed to extract fingerprint util from archive: %v", err)
            return
        }
        defer src.Close()

        dst, err := os.OpenFile(pathToFpUtil(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
        if err != nil {
            utils.Log(utils.ERROR, "recognizer.extractFpUtil: failed to open file '%v' for writing: %v", pathToFpUtil(), err)
            return
        }
        defer dst.Close()

        if _, err := io.Copy(dst, src); err != nil {
            utils.Log(utils.ERROR, "recognizer.extractFpUtil: failed to save fingerprint util to '%v': %v", pathToFpUtil(), err)
            return
        }

        break
    }
}

开发者ID:mzinin,项目名称:tagger,代码行数:34,代码来源:fingerprint_windows.go

示例11: unpackZip

func unpackZip(dest string, r *os.File) error {
    stat, err := r.Stat()
    if err != nil {
        return err
    }
    zr, err := zip.NewReader(r, stat.Size())
    if err != nil {
        return err
    }
    for _, f := range zr.File {
        if strings.HasSuffix(f.Name, "/") {
            continue
        }
        fr, err := f.Open()
        if err != nil {
            return err
        }
        err = unpackFile(dest, fr, f.Name, f.Mode())
        fr.Close()
        if err != nil {
            return err
        }
    }
    return nil
}

开发者ID:devick,项目名称:flynn,代码行数:25,代码来源:unpack.go

示例12: setup

// setups state each of the tests uses
func setup() error {
    // create zipfs
    b := new(bytes.Buffer)
    zw := zip.NewWriter(b)
    for file, contents := range files {
        w, err := zw.Create(file)
        if err != nil {
            return err
        }
        _, err = io.WriteString(w, contents)
        if err != nil {
            return err
        }
    }
    zw.Close()
    zr, err := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
    if err != nil {
        return err
    }
    rc := &zip.ReadCloser{
        Reader: *zr,
    }
    fs = New(rc, "foo")

    // pull out different stat functions
    statFuncs = []statFunc{
        {"Stat", fs.Stat},
        {"Lstat", fs.Lstat},
    }

    return nil
}

开发者ID:Requilence,项目名称:integram,代码行数:33,代码来源:zipfs_test.go

示例13: getLatestVersion

func getLatestVersion(zipFile []byte, prefix, suffix string) (string, error) {
    r, err := zip.NewReader(bytes.NewReader(zipFile), int64(len(zipFile)))
    if err != nil {
        return "", err
    }
    latest := ""
    for _, f := range r.File {
        artifact := path.Base(f.Name)
        if strings.HasPrefix(artifact, prefix) && strings.HasSuffix(artifact, suffix) {
            v := extractVersion(artifact)
            if latest == "" {
                latest = v
            }
            v1, err := semver.Parse(v)
            if err != nil {
                return "", err
            }
            l := semver.MustParse(latest)
            if v1.GT(l) {
                latest = v
            }
        }
    }
    return latest, nil
}

开发者ID:fikridroid,项目名称:gradleplease,代码行数:25,代码来源:get_versions.go

示例14: TestCopyZipFile

func (s *S) TestCopyZipFile(c *gocheck.C) {
    tempDir, err := ioutil.TempDir("", "TestCopyZipFileDir")
    defer func() {
        os.RemoveAll(tempDir)
    }()
    c.Assert(err, gocheck.IsNil)
    var files = []File{
        {"doge.txt", "Much doge"},
        {"much.txt", "Much mucho"},
        {"WOW/WOW.WOW1", "WOW\nWOW"},
        {"WOW/WOW.WOW2", "WOW\nWOW"},
        {"/usr/WOW/WOW.WOW3", "WOW\nWOW"},
        {"/usr/WOW/WOW.WOW4", "WOW\nWOW"},
    }
    buf, err := CreateZipBuffer(files)
    c.Assert(err, gocheck.IsNil)
    c.Assert(buf, gocheck.NotNil)
    r, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
    for _, f := range r.File {
        err = CopyZipFile(f, tempDir, f.Name)
        c.Assert(err, gocheck.IsNil)
        fstat, errStat := os.Stat(path.Join(tempDir, f.Name))
        c.Assert(errStat, gocheck.IsNil)
        c.Assert(fstat.IsDir(), gocheck.Equals, false)
    }
}

开发者ID:rfloriano,项目名称:gandalf,代码行数:26,代码来源:multipartzip_test.go

示例15: Execute

func (t *unzip) Execute() error {
    zipFile, err := os.Open(t.zipFileName)
    if err != nil {
        return makeError("opening file '"+t.zipFileName+"'", err)
    }
    defer zipFile.Close()

    info, err := zipFile.Stat()
    if err != nil {
        return makeError("reading file info of '"+zipFile.Name()+"'", err)
    }

    zipReader, err := zip.NewReader(zipFile, info.Size())
    if err != nil {
        return makeError("creating zip file reader for '"+zipFile.Name()+"'", err)
    }

    for _, f := range zipReader.File {
        destPath := filepath.Join(t.path, filepath.FromSlash(f.Name))
        if f.FileInfo().IsDir() {
            if err := os.MkdirAll(destPath, 0666); err != nil {
                return makeError("creating path to folder '"+destPath+"'", err)
            }
        } else {
            if err := copyFile(f, destPath); err != nil {
                return makeError(
                    "copying zip file '"+f.Name+"' data to file '"+destPath+"'",
                    err)
            }
        }
    }

    return nil
}

开发者ID:gonutz,项目名称:installer,代码行数:34,代码来源:unzip.go

示例16: TestZipWithDirectory

func TestZipWithDirectory(t *testing.T) {
    dir, err := os.Getwd()
    assert.NoError(t, err)

    zipper := ApplicationZipper{}
    zipFile, err := zipper.Zip(filepath.Clean(dir + "/../fixtures/zip/"))
    assert.NoError(t, err)

    byteReader := bytes.NewReader(zipFile.Bytes())
    reader, err := zip.NewReader(byteReader, int64(byteReader.Len()))
    assert.NoError(t, err)

    readFile := func(index int) (string, string) {
        buf := &bytes.Buffer{}
        file := reader.File[index]
        fReader, err := file.Open()
        _, err = io.Copy(buf, fReader)

        assert.NoError(t, err)

        return file.Name, string(buf.Bytes())
    }

    assert.Equal(t, len(reader.File), 2)

    name, contents := readFile(0)
    assert.Equal(t, name, "foo.txt")
    assert.Equal(t, contents, "This is a simple text file.")

    name, contents = readFile(1)
    assert.Equal(t, name, "subDir/bar.txt")
    assert.Equal(t, contents, "I am in a subdirectory.")
}

开发者ID:KaiYoung,项目名称:cli,代码行数:33,代码来源:zipper_test.go

示例17: uploadBuildpackRequest

func uploadBuildpackRequest(filename string) testnet.TestRequest {
    return testnet.TestRequest{
        Method: "PUT",
        Path:   "/v2/buildpacks/my-cool-buildpack-guid/bits",
        Response: testnet.TestResponse{
            Status: http.StatusCreated,
            Body:   `{ "metadata":{ "guid": "my-job-guid" } }`,
        },
        Matcher: func(request *http.Request) {
            err := request.ParseMultipartForm(4096)
            defer request.MultipartForm.RemoveAll()
            Expect(err).NotTo(HaveOccurred())

            Expect(len(request.MultipartForm.Value)).To(Equal(0))
            Expect(len(request.MultipartForm.File)).To(Equal(1))

            files, ok := request.MultipartForm.File["buildpack"]
            Expect(ok).To(BeTrue(), "Buildpack file part not present")
            Expect(len(files)).To(Equal(1), "Wrong number of files")

            buildpackFile := files[0]
            Expect(buildpackFile.Filename).To(Equal(filepath.Base(filename)), "Wrong file name")

            file, err := buildpackFile.Open()
            Expect(err).NotTo(HaveOccurred())

            zipReader, err := zip.NewReader(file, 4096)
            Expect(err).NotTo(HaveOccurred())

            actualFileNames := []string{}
            actualFileContents := []string{}
            for _, f := range zipReader.File {
                actualFileNames = append(actualFileNames, f.Name)
                c, _ := f.Open()
                content, _ := ioutil.ReadAll(c)
                actualFileContents = append(actualFileContents, string(content))
            }
            sort.Strings(actualFileNames)

            Expect(actualFileNames).To(Equal([]string{
                "bin/compile",
                "bin/detect",
                "bin/release",
                "lib/helper",
            }))
            Expect(actualFileContents).To(Equal([]string{
                "the-compile-script\n",
                "the-detect-script\n",
                "the-release-script\n",
                "the-helper-script\n",
            }))

            if runtime.GOOS != "windows" {
                Expect(zipReader.File[0].Mode()).To(Equal(os.FileMode(0755)))
                Expect(zipReader.File[1].Mode()).To(Equal(os.FileMode(0755)))
                Expect(zipReader.File[2].Mode()).To(Equal(os.FileMode(0755)))
            }
        },
    }
}

开发者ID:normalnorman,项目名称:cli,代码行数:60,代码来源:buildpack_bits_test.go

示例18: GetFileFromZip

func GetFileFromZip(file io.ReaderAt, size int64, subFile string) []byte {
    r, err := zip.NewReader(file, size)
    if err != nil {
        log.Fatal(err)
    }

    // Iterate through the files in the archive,
    // printing some of their contents.
    for _, f := range r.File {
        if f.Name == subFile {
            var body []byte
            rc, err := f.Open()
            if err != nil {
                log.Fatal(err)
            }
            body, err = ioutil.ReadAll(rc)
            if err != nil {
                log.Fatal(err)
            }
            rc.Close()

            return body
        }
    }
    return []byte{}
}

开发者ID:yashodhank,项目名称:rats,代码行数:26,代码来源:common.go

示例19: installTerminalNotifier

func installTerminalNotifier() error {
    baseRootPath := filepath.Join(os.TempDir(), tempDirSuffix)
    rootPath = filepath.Join(baseRootPath, zipNameSuffix)

    //if terminal-notifier.app already installed no-need to re-install
    if exists(filepath.Join(rootPath, executablePath)) {
        return nil
    }
    buf := bytes.NewReader(terminalnotifier())
    reader, err := zip.NewReader(buf, int64(buf.Len()))
    if err != nil {
        return err
    }
    err = unpackZip(reader, baseRootPath)
    if err != nil {
        return fmt.Errorf("could not unpack zip terminal-notifier file: %s", err)
    }

    err = os.Chmod(filepath.Join(rootPath, executablePath), 0755)
    if err != nil {
        return fmt.Errorf("could not make terminal-notifier executable: %s", err)
    }

    return nil
}

开发者ID:twstrike,项目名称:coyim,代码行数:25,代码来源:terminal-app-zip.go

示例20: loadMap

func loadMap() (map[string]Resource, error) {
    this := os.Args[0]
    file, err := os.Open(this)
    if err != nil {
        return nil, err
    }

    info, err := file.Stat()
    if err != nil {
        return nil, err
    }
    rdr, err := zip.NewReader(file, info.Size())
    if err != nil {
        return nil, err
    }

    entries := make(map[string]Resource)
    for _, file := range rdr.File {
        if file.FileInfo().IsDir() {
            continue
        }
        entries[file.Name] = &resource{file}
    }

    return entries, nil
}

开发者ID:CasualSuperman,项目名称:goembed,代码行数:26,代码来源:gotogether.go

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