Golang archive-zip.NewReader类(方法)实例源码

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

作者:jameine    项目:cor   
// 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
}

作者:Skarls    项目:slic   
// 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())
}

作者:anyka    项目:   
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
}

作者:Skarls    项目:slic   
// 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())
}

作者:getcarin    项目:libcarin   
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()))
}

作者:hobinj    项目:go-gette   
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
}

作者:itchi    项目:whar   
// 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)
}

作者:kenp    项目:gandal   
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])
	}
}

作者:Lin4ipsu    项目:cl   
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
}

作者:mzini    项目:tagge   
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
	}
}

作者:devic    项目:flyn   
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
}

作者:Requilenc    项目:integra   
// 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
}

作者:fikridroi    项目:gradlepleas   
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
}

作者:rflorian    项目:gandal   
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)
	}
}

作者:gonut    项目:installe   
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
}

作者:KaiYoun    项目:cl   
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.")
}

作者:normalnorma    项目:cl   
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)))
			}
		},
	}
}

作者:yashodhan    项目:rat   
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{}
}

作者:twstrik    项目:coyi   
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
}

作者:CasualSuperma    项目:goembe   
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
}


问题


面经


文章

微信
公众号

扫码关注公众号