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) } } }
// 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 }
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 }
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("----------------------") } }
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() }
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. }
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 }
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 }
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 }
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!") }
// 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 }
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 }
// 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 }
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() } }
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 }
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 }
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) }
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 }
//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 }
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 }
//LoadAFM loads a, possible zipped, FeatureMatrix specified by filename func LoadAFM(filename string) (fm *FeatureMatrix, err error) { r, err := zip.OpenReader(filename) if err == nil { rc, err := r.File[0].Open() if err == nil { fm = ParseAFM(rc) rc.Close() return fm, err } } datafile, err := os.Open(filename) if err != nil { return } switch { case strings.HasSuffix(filename, ".arff"): fm = ParseARFF(datafile) case strings.HasSuffix(filename, ".libsvm"): fm = ParseLibSVM(datafile) default: fm = ParseAFM(datafile) } datafile.Close() return }
func DeCompressZip() { const File = "/home/figo/develop/env/GOPATH/src/github.com/figoxu/goPraticse/fileCode.zip" const dir = "/home/figo/delete/tmp/" os.Mkdir(dir, 0777) //创建一个目录 cf, err := zip.OpenReader(File) //读取zip文件 if err != nil { fmt.Println(err) } defer cf.Close() for _, file := range cf.File { rc, err := file.Open() if err != nil { fmt.Println(err) } f, err := os.Create(dir + file.Name) if err != nil { fmt.Println(err) } defer f.Close() n, err := io.Copy(f, rc) if err != nil { fmt.Println(err) } fmt.Println(n) } }
func words100k() []string { result := []string{} r, err := zip.OpenReader(fullPath("words100k.txt.zip")) if err != nil { panic(err) } defer safeClose(r) f := r.File[0] rc, err := f.Open() if err != nil { panic(err) } defer safeClose(rc) scanner := bufio.NewScanner(rc) for scanner.Scan() { result = append(result, scanner.Text()) } if scanner.Err() != nil { panic(scanner.Err()) } return result }
// Zips up the keys in the old repo, and assert that we can import it and use // said keys. The 0.1 exported format is just a zip file of all the keys func TestImport0Dot1Zip(t *testing.T) { ks, ret, gun := get0Dot1(t) zipFile, err := ioutil.TempFile("", "notary-test-zipFile") defer os.RemoveAll(zipFile.Name()) zipWriter := zip.NewWriter(zipFile) assert.NoError(t, err) assert.NoError(t, addKeysToArchive(zipWriter, ks)) zipWriter.Close() zipFile.Close() origKeys := ks.ListKeys() assert.Len(t, origKeys, 3) // now import the zip file into a new cryptoservice tempDir, err := ioutil.TempDir("", "notary-test-import") defer os.RemoveAll(tempDir) assert.NoError(t, err) ks, err = trustmanager.NewKeyFileStore(tempDir, ret) assert.NoError(t, err) cs := NewCryptoService(gun, ks) zipReader, err := zip.OpenReader(zipFile.Name()) assert.NoError(t, err) defer zipReader.Close() assert.NoError(t, cs.ImportKeysZip(zipReader.Reader)) assertHasKeys(t, cs, origKeys) }
func readCorpus() []*prog.Prog { if *flagCorpus == "" { return nil } zipr, err := zip.OpenReader(*flagCorpus) if err != nil { Fatalf("failed to open bin file: %v", err) } var progs []*prog.Prog for _, zipf := range zipr.File { r, err := zipf.Open() if err != nil { Fatalf("failed to uzip file from input archive: %v", err) } data, err := ioutil.ReadAll(r) if err != nil { Fatalf("failed to read corpus file: %v", err) } p, err := prog.Deserialize(data) if err != nil { Fatalf("failed to deserialize corpus program: %v", err) } progs = append(progs, p) r.Close() } zipr.Close() return progs }
// Export all the keys of a cryptoservice to a zipfile, and import it into a // new cryptoService, and return that new cryptoService func importExportedZip(t *testing.T, original *CryptoService, ret passphrase.Retriever, gun string) (*CryptoService, string) { // Temporary directory where test files will be created tempBaseDir, err := ioutil.TempDir("", "notary-test-") assert.NoError(t, err, "failed to create a temporary directory: %s", err) ks, err := trustmanager.NewKeyFileStore(tempBaseDir, ret) assert.NoError(t, err) var cs *CryptoService // export keys zipFile, err := ioutil.TempFile("", "notary-test-zipFile") defer os.RemoveAll(zipFile.Name()) if gun != "" { original.ExportKeysByGUN(zipFile, gun, ret) cs = NewCryptoService(gun, ks) } else { original.ExportAllKeys(zipFile, ret) cs = NewCryptoService(original.gun, ks) } zipFile.Close() // import keys into the cryptoservice now zipReader, err := zip.OpenReader(zipFile.Name()) assert.NoError(t, err) defer zipReader.Close() assert.NoError(t, cs.ImportKeysZip(zipReader.Reader)) return cs, tempBaseDir }
func (z *ZipArchiveProvider) Extract(dst, src string) error { rc, err := zip.OpenReader(src) if err != nil { return err } for _, zf := range rc.File { fp := filepath.Join(dst, zf.Name) if zf.FileInfo().IsDir() { os.MkdirAll(fp, 0755) continue } os.MkdirAll(filepath.Dir(fp), 0755) zfr, err := zf.Open() if err != nil { return err } f, err := os.Create(fp) if err != nil { zfr.Close() return err } _, err = io.Copy(f, zfr) f.Close() zfr.Close() if err != nil { return err } os.Chmod(fp, zf.Mode()) } return nil }
func main() { // Create a reader out of the zip archive zipReader, err := zip.OpenReader("test.zip") if err != nil { log.Fatal(err) } defer zipReader.Close() // Iterate through each file/dir found in for _, file := range zipReader.Reader.File { // Open the file inside the zip archive // like a normal file zippedFile, err := file.Open() if err != nil { log.Fatal(err) } defer zippedFile.Close() // Specify what the extracted file name should be. // You can specify a full path or a prefix // to move it to a different directory. // In this case, we will extract the file from // the zip to a file of the same name. targetDir := "./" extractedFilePath := filepath.Join( targetDir, file.Name, ) // Extract the item (or create directory) if file.FileInfo().IsDir() { // Create directories to recreate directory // structure inside the zip archive. Also // preserves permissions log.Println("Creating directory:", extractedFilePath) os.MkdirAll(extractedFilePath, file.Mode()) } else { // Extract regular file since not a directory log.Println("Extracting file:", file.Name) // Open an output file for writing outputFile, err := os.OpenFile( extractedFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode(), ) if err != nil { log.Fatal(err) } defer outputFile.Close() // "Extract" the file by copying zipped file // contents to the output file _, err = io.Copy(outputFile, zippedFile) if err != nil { log.Fatal(err) } } } }
func Unzip(src, dest string) error { r, err := zip.OpenReader(src) if err != nil { return err } defer r.Close() for _, f := range r.File { rc, err := f.Open() if err != nil { return err } defer rc.Close() path := filepath.Join(dest, f.Name) if f.FileInfo().IsDir() { os.MkdirAll(path, f.Mode()) } else { f, err := os.OpenFile( path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { return err } defer f.Close() _, err = io.Copy(f, rc) if err != nil { return err } } } return nil }
func (feed *Feed) getFile(path string, name string) (io.Reader, error) { fileInfo, err := os.Stat(path) if err != nil { return nil, err } if fileInfo.IsDir() { if feed.curFileHandle != nil { // close previous handle feed.curFileHandle.Close() } return os.Open(opath.Join(path, name)) } else { var e error if feed.zipFileCloser == nil { // reuse existing opened zip file feed.zipFileCloser, e = zip.OpenReader(path) } if e != nil { return nil, e } for _, f := range feed.zipFileCloser.File { if f.Name == name { return f.Open() } } } return nil, errors.New("Not found.") }