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

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

作者:anyka    项目:   
func main() {
	var output string
	flag.StringVar(&output, "file", "", "Output result to this file")
	flag.Parse()

	if flag.NArg() < 2 {
		webui()
		os.Exit(1)
	} else {
		zip1, err := zip.OpenReader(flag.Arg(0))
		if err != nil {
			log.Fatal(err)
		}
		zip2, err := zip.OpenReader(flag.Arg(1))
		if err != nil {
			log.Fatal(err)
		}
		defer zip1.Close()
		defer zip2.Close()
		result := ZipDiffContent(&zip1.Reader, &zip2.Reader, false, "")
		if output != "" {
			f, err := os.Create(output)
			if err != nil {
				log.Fatal(err)
			}
			defer f.Close()
			DisplayAsText(f, result)
		} else {
			DisplayAsText(os.Stdout, result)
		}
	}
}

作者:theseusyan    项目:occul   
// Write data to store
func writeData(fn string, nodeID int) (dbTrain, dbTest string) {

	gob.Register(Obs{})

	// Open a zip archive for reading.
	r, err := zip.OpenReader(fn)
	if err != nil {
		glog.Fatalf("can't open zip file %s - error: %s", fn, err)
	}
	defer r.Close()

	// Iterate through the files in the archive,
	for _, f := range r.File {
		fn := path.Base(f.Name)
		if fn == TrainFile {
			glog.Infof("Found %s\n", f.Name)
			dbTrain = writeStore(f, nodeID)
		}
		if fn == TestFile {
			glog.Infof("Found %s\n", f.Name)
			dbTest = writeStore(f, nodeID)
		}
	}
	return
}

作者:urs    项目:beat   
func unzip(archive, target string) error {

	fmt.Println("Unzip archive ", target)

	reader, err := zip.OpenReader(archive)
	if err != nil {
		return err
	}

	for _, file := range reader.File {
		filePath := filepath.Join(target, file.Name)

		if file.FileInfo().IsDir() {
			os.MkdirAll(filePath, file.Mode())
			continue
		}
		fileReader, err := file.Open()
		if err != nil {
			return err
		}
		defer fileReader.Close()

		targetFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
		if err != nil {
			return err
		}
		defer targetFile.Close()

		if _, err := io.Copy(targetFile, fileReader); err != nil {
			return err
		}
	}
	return nil
}

作者:ericcapricor    项目:GoStandardLibrary-Chines   
func main() {
	zipFileName := "test.zip"

	rc, err := zip.OpenReader(zipFileName)
	if err != nil {
		log.Fatal(err)
	}

	zipFiles := rc.File
	for _, zipFile := range zipFiles {
		offset, _ := zipFile.DataOffset()
		log.Println("Compresszed:", zipFile.FileHeader.CompressedSize64, "Uncompressed:", zipFile.FileHeader.UncompressedSize64)
		log.Println(zipFile.FileHeader.Name, "\tDataOffset", offset)

		irc, err := zipFile.Open()
		if err != nil {
			log.Fatal(err)
		}

		if fileContent, err := ioutil.ReadAll(irc); err == nil {
			data := string(fileContent)
			fmt.Print(len(data), "\t", data)
		} else {
			log.Fatal(err)
		}
		irc.Close()
		fmt.Println("----------------------")
	}
}

作者:Richardph    项目:nom   
func (f *Fuzzy) TestRandomBrokenParts(c *C) {
	if testing.Short() {
		c.Log("This test, tests many versions of an xlsx file and might take a while, it is being skipped")
		c.SucceedNow()
	}
	log.Println("Fuzzy test is using this -test.seed=" + strconv.FormatInt(*randseed, 10))
	rand.Seed(*randseed)
	template, err := zip.OpenReader("./testdocs/testfile.xlsx")
	c.Assert(err, IsNil)
	defer template.Close()

	count := 0

	for brokenFile := range generateBrokenFiles(&template.Reader) {
		count++
		if testing.Verbose() {
			//If the library panics fatally it would be nice to know why
			log.Println("Testing change to ", brokenFile.Name, " on token ", tokenToString(brokenFile.Old), " of type ", reflect.TypeOf(brokenFile.Old), " to ", tokenToString(brokenFile.New))
		}

		if e := Raises(func() { ReadZipReader(brokenFile.File) }); e != nil {

			c.Log("Some file with random changes did raise an exception instead of returning an error", e)
			c.Log("Testing change to ", brokenFile.Name, " on token ", tokenToString(brokenFile.Old), " of type ", reflect.TypeOf(brokenFile.Old), " to ", tokenToString(brokenFile.New))
			c.FailNow()
		}

	}
	c.Succeed()
}

作者:enderl    项目:vf   
func ExampleReader() {
	// Open a zip archive for reading.
	r, err := zip.OpenReader(`c:\xx.zip`)
	if err != nil {
		log.Fatal(err)
	}
	defer r.Close()

	// Iterate through the files in the archive,
	// printing some of their contents.
	for _, f := range r.File {
		fmt.Printf("Contents of %s:\n", f.Name)
		rc, err := f.Open()
		if err != nil {
			fmt.Println("\nopen err:", err)
		}
		_, err = io.CopyN(os.Stdout, rc, 68)
		if err != nil {
			fmt.Println("\ncopy err:", err)
		}
		rc.Close()
		fmt.Println()
	}
	// Output:
	// Contents of README:
	// This is the source code repository for the Go programming language.
}

作者:0x9e3779b    项目:golang_la   
func UnZip(zipFile string) (dirName string, err error) {
	r, err := zip.OpenReader(zipFile)
	if err != nil {
		return
	}

	for _, k := range r.Reader.File {
		if k.FileInfo().IsDir() {
			name := filepath.Join(TMPDIR, k.Name)
			err := os.MkdirAll(name, 0664)
			if err != nil {
				return "", err
			}
			dirName = k.Name
			continue
		}
		r, err := k.Open()
		if err != nil {
			return "", err
		}
		defer r.Close()
		NewFile, err := os.OpenFile(filepath.Join(TMPDIR, k.Name), os.O_TRUNC|os.O_RDWR|os.O_CREATE, 0664)
		if err != nil {
			return "", err
		}
		io.Copy(NewFile, r)
		NewFile.Close()
	}
	return
}

作者:RainYang092    项目:gohttpserve   
func parseIPA(path string) (plinfo *plistBundle, err error) {
	plistre := regexp.MustCompile(`^Payload/[^/]*/Info\.plist$`)
	r, err := zip.OpenReader(path)
	if err != nil {
		return
	}
	defer r.Close()

	var plfile *zip.File
	for _, file := range r.File {
		if plistre.MatchString(file.Name) {
			plfile = file
			break
		}
	}
	if plfile == nil {
		err = errors.New("Info.plist file not found")
		return
	}
	plreader, err := plfile.Open()
	if err != nil {
		return
	}
	defer plreader.Close()
	buf := make([]byte, plfile.FileInfo().Size())
	_, err = io.ReadFull(plreader, buf)
	if err != nil {
		return
	}
	dec := goplist.NewDecoder(bytes.NewReader(buf))
	plinfo = new(plistBundle)
	err = dec.Decode(plinfo)
	return
}

作者:leonardy    项目:mobil   
func apiResources() ([]byte, error) {
	p, err := apiJarPath()
	if err != nil {
		return nil, err
	}
	zr, err := zip.OpenReader(p)
	if err != nil {
		return nil, err
	}
	defer zr.Close()

	buf := new(bytes.Buffer)
	for _, f := range zr.File {
		if f.Name == "resources.arsc" {
			rc, err := f.Open()
			if err != nil {
				return nil, err
			}
			_, err = io.Copy(buf, rc)
			if err != nil {
				return nil, err
			}
			rc.Close()
			break
		}
	}
	if buf.Len() == 0 {
		return nil, fmt.Errorf("failed to read resources.arsc")
	}
	return buf.Bytes(), nil
}

作者:keb    项目:gorgas   
func loadAsset(activity unsafe.Pointer, filename string) (io.Reader, error) {
	apkPath := C.GoString(C.getPackageName((*C.ANativeActivity)(activity)))

	// Open a zip archive for reading.
	r, err := zip.OpenReader(apkPath)
	if err != nil {
		return nil, err
	}
	defer r.Close()

	// Iterate through the files in the archive.
	for _, f := range r.File {
		if f.Name == filename {
			rc, err := f.Open()
			if err != nil {
				return nil, err
			}
			buffer := make([]byte, f.UncompressedSize64)
			_, err = io.ReadFull(rc, buffer)
			if err != nil {
				return nil, err
			}
			rc.Close()
			return bytes.NewBuffer(buffer), nil
		}
	}
	return nil, fmt.Errorf("Resource not found!")
}

作者:than    项目:go-read-a-de   
// ReadAPK opens the specified APK file 'apk' and walks the contents
// of any DEX files it contains, making callbacks at various points
// through a user-supplied visitor object 'visitor'. See DexApkVisitor
// for more info on which DEX/APK parts are visited.
func ReadAPK(apk string, visitor DexApkVisitor) error {
	rc, err := zip.OpenReader(apk)
	if err != nil {
		return errors.New(fmt.Sprintf("unable to open APK %s: %v", apk, err))
	}
	defer rc.Close()
	z := &rc.Reader

	visitor.VisitAPK(apk)
	visitor.Verbose(1, "APK %s contains %d entries", apk, len(z.File))

	isDex := regexp.MustCompile(`^\S+\.dex$`)
	for i := 0; i < len(z.File); i++ {
		entryName := z.File[i].Name
		if isDex.MatchString(entryName) {
			visitor.Verbose(1, "dex file %s at entry %d", entryName, i)
			reader, err := z.File[i].Open()
			if err != nil {
				return errors.New(fmt.Sprintf("opening apk %s dex %s: %v", apk, entryName, err))
			}
			func() {
				dexread.ReadDEX(&apk, entryName, reader,
					z.File[i].UncompressedSize64, visitor)
				defer reader.Close()
			}()
		}
	}
	return nil
}

作者:steveye    项目:cbg   
func zipStaticServe(fn string, lastTs time.Time) (*zipHandler, error) {
	zr, err := zip.OpenReader(fn)
	if err != nil {
		return nil, err
	}
	defer zr.Close()

	zf := &zipHandler{
		ts:   lastTs,
		data: map[string][]byte{},
	}

	for _, f := range zr.File {
		rc, err := f.Open()
		if err != nil {
			return nil, err
		}

		d, err := ioutil.ReadAll(rc)
		if err != nil {
			return nil, err
		}
		rc.Close()

		zf.data[f.Name] = d
	}

	return zf, nil
}

作者:knierie    项目:od   
// Open an Open Document File, uncompress its ZIP archive,
// and check its mime type. The returned *File provides --
// via its Open method -- access to files containted in the
// ZIP file, like content.xml.
func Open(odfName string) (f *File, err error) {
	of := new(File)
	of.ReadCloser, err = zip.OpenReader(odfName)
	if err != nil {
		return
	}
	mf, err := of.Open("mimetype")
	if err != nil {
		of.Close()
		return
	}
	defer mf.Close()

	b, err := ioutil.ReadAll(mf)
	if err != nil {
		return
	}
	of.MimeType = string(b)

	if !strings.HasPrefix(of.MimeType, MimeTypePfx) {
		err = errors.New("not an Open Document mime type")
	} else {
		f = of
	}

	return
}

作者:qt-luig    项目:golangcaf   
func main() {
	reader, err := zip.OpenReader("output/sample.zip")
	if err != nil {
		log.Fatalln(err)
	}
	defer reader.Close()

	var rc io.ReadCloser
	for _, f := range reader.File {
		rc, err = f.Open()
		if err != nil {
			log.Fatal(err)
		}

		buf := new(bytes.Buffer)
		_, err = io.Copy(buf, rc)
		if err != nil {
			log.Fatal(err)
		}
		if err = ioutil.WriteFile("output/"+f.Name, buf.Bytes(), 0755); err != nil {
			log.Fatal(err)
		}
		rc.Close()
	}
}

作者:infoassur    项目:nsrl   
func UnzipFile(src, dest, fileName string) error {
	zipReader, err := zip.OpenReader(src)
	if err != nil {
		return err
	}
	defer func() error {
		if err := zipReader.Close(); err != nil {
			return fmt.Errorf("Error unzipping: (%s) %v", src, err)
		}

		return nil
	}()

	for _, zf := range zipReader.File {
		if zf.Name != fileName {
			continue
		}

		dst, err := os.Create(dest)
		if err != nil {
			return err
		}

		defer dst.Close()
		src, err := zf.Open()
		if err != nil {
			// err
		}
		defer src.Close()

		io.Copy(dst, src)
	}

	return nil
}

作者:jsl    项目:gtbo   
func ExtractZipFile(src string, dest string) error {
	if !strings.HasSuffix(dest, pathutil.SLASH) {
		dest += pathutil.SLASH
	}
	rc, err := zip.OpenReader(src)
	if err != nil {
		return err
	}
	defer rc.Close()

	for _, zf := range rc.Reader.File {
		name := zf.Name
		mode := zf.Mode()
		if mode.IsDir() {
			os.MkdirAll(dest+name, zf.Mode())
		} else {
			parent_path := pathutil.ParentPath(dest + name)
			os.MkdirAll(parent_path, pathutil.DEFAULT_DIR_ACCESS)
			if err := unpackZippedFile(dest, name, zf); err != nil {
				return err
			}
			os.Chmod(dest+name, mode)
		}
	}

	return nil
}

作者:RainYang092    项目:gohttpserve   
func parseIpaIcon(path string) (data []byte, err error) {
	iconPattern := regexp.MustCompile(`(?i)^Payload/[^/]*/icon\.png$`)
	r, err := zip.OpenReader(path)
	if err != nil {
		return
	}
	defer r.Close()

	var zfile *zip.File
	for _, file := range r.File {
		if iconPattern.MatchString(file.Name) {
			zfile = file
			break
		}
	}
	if zfile == nil {
		err = errors.New("icon.png file not found")
		return
	}
	plreader, err := zfile.Open()
	if err != nil {
		return
	}
	defer plreader.Close()
	return ioutil.ReadAll(plreader)
}

作者:rootwzr    项目:appstax-cl   
func unzip(src string, dest string) error {
	zipReader, err := zip.OpenReader(src)
	if err != nil {
		return err
	}
	defer zipReader.Close()

	os.MkdirAll(dest, 0755)
	for _, file := range zipReader.File {
		reader, err := file.Open()
		if err != nil {
			return err
		}
		defer reader.Close()

		path := filepath.Join(dest, file.Name)
		if file.FileInfo().IsDir() {
			os.MkdirAll(path, file.Mode())
		} else {
			file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
			if err != nil {
				return err
			}

			_, err = io.Copy(file, reader)
			if err != nil {
				return err
			}
			file.Close()
		}
	}

	return nil
}

作者:kuba-    项目:c-go-zi   
//export Unzip
func Unzip(zipname *C.char) int32 {
	r, err := zip.OpenReader(C.GoString(zipname))
	if err != nil {
		log.Println(err)
		return -1
	}
	defer r.Close()

	// Iterate through the files in the archive,
	for _, file := range r.File {
		ri, err := file.Open()
		if err != nil {
			log.Println(err)
			return -1
		}
		defer ri.Close()

		wi, err := os.Create(file.Name)
		if err != nil {
			log.Println(err)
			return -1
		}
		defer wi.Close()

		_, err = io.Copy(wi, ri)
		if err != nil {
			log.Println(err)
			return -1
		}
	}

	return 0
}

作者:mehmet    项目:prerunserve   
func Unzip(src string) string {
	var fileName string
	r, err := zip.OpenReader(src)
	if err != nil {
		panic(err)
	}
	defer r.Close()

	for _, f := range r.File {
		if f.Name == "ngrok" {
			fileName = f.Name
			rc, err := f.Open()
			if err != nil {
				panic(err)
			}
			defer rc.Close()

			f, err := os.OpenFile(
				f.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
			if err != nil {
				panic(err)
			}
			defer f.Close()

			_, err = io.Copy(f, rc)
			if err != nil {
				panic(err)
			}
		}
	}

	return fileName
}


问题


面经


文章

微信
公众号

扫码关注公众号