// 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 }
// 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()) }
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 }
// 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()) }
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())) }
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 }
// 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) }
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]) } }
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 }
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 } }
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 }
// 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 }
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 }
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) } }
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 }
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.") }
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))) } }, } }
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{} }
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 }
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 }
func downloadFile(pluginName, filePath, url string) (err error) { defer func() { if r := recover(); r != nil { retryCount++ if retryCount < 3 { fmt.Println("Failed downloading. Will retry once.") err = downloadFile(pluginName, filePath, url) } else { failure := fmt.Sprintf("%v", r) if failure == "runtime error: makeslice: len out of range" { err = fmt.Errorf("Corrupt http response from source. Please try again.\n") } else { panic(r) } } } }() resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return err } r, err := zip.NewReader(bytes.NewReader(body), resp.ContentLength) if err != nil { return err } for _, zf := range r.File { newFile := path.Join(filePath, RemoveGitBuildFromName(pluginName, zf.Name)) if zf.FileInfo().IsDir() { err := os.Mkdir(newFile, 0777) if PermissionsError(err) { return fmt.Errorf(permissionsDeniedMessage, newFile) } } else { dst, err := os.Create(newFile) if PermissionsError(err) { return fmt.Errorf(permissionsDeniedMessage, newFile) } src, err := zf.Open() if err != nil { log.Errorf("Failed to extract file: %v", err) } io.Copy(dst, src) dst.Close() src.Close() } } return nil }
func (ga *githubAccess) getZipArchive() (zipReader *zip.Reader, err error) { var archiveURL *url.URL var httpResponse *http.Response var zipBytes []byte err = ga.parseURL() if err != nil { return nil, err } archiveURL, _, err = ga.client.Repositories.GetArchiveLink(ga.owner, ga.repos, github.Zipball, nil) if err != nil { return nil, err } httpResponse, err = http.Get(archiveURL.String()) if err != nil { return nil, err } zipBytes, err = ioutil.ReadAll(httpResponse.Body) if err != nil { return nil, err } zipReader, err = zip.NewReader(bytes.NewReader(zipBytes), int64(len(zipBytes))) if err != nil { return nil, err } return }
// OpenReaderAt() take io.ReaderAt of an XLSX file and returns a populated // xlsx.File struct for it. func OpenReaderAt(r io.ReaderAt, size int64) (*File, error) { file, err := zip.NewReader(r, size) if err != nil { return nil, err } return ReadZipReader(file) }
func loadTestData() []Test { f := openReader(*testdata) buffer, err := ioutil.ReadAll(f) f.Close() Error(err) archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer))) Error(err) tests := []Test{} for _, f := range archive.File { // Skip the short versions, which are simply duplicates of the long versions. if strings.Contains(f.Name, "SHORT") || f.FileInfo().IsDir() { continue } ff, err := f.Open() Error(err) defer ff.Close() input := bufio.NewReader(ff) test := Test{name: path.Base(f.Name)} for { line, err := input.ReadString('\n') if err != nil { if err == io.EOF { break } log.Fatal(err) } if len(line) <= 1 || line[0] == '#' { if m := versionRe.FindStringSubmatch(line); m != nil { if m[1] != unicode.Version { log.Printf("warning:%s: version is %s; want %s", f.Name, m[1], unicode.Version) } } continue } m := testRe.FindStringSubmatch(line) if m == nil || len(m) < 3 { log.Fatalf(`Failed to parse: "%s" result: %#v`, line, m) } str := []byte{} // In the regression test data (unpaired) surrogates are assigned a weight // corresponding to their code point value. However, utf8.DecodeRune, // which is used to compute the implicit weight, assigns FFFD to surrogates. // We therefore skip tests with surrogates. This skips about 35 entries // per test. valid := true for _, split := range strings.Split(m[1], " ") { r, err := strconv.ParseUint(split, 16, 64) Error(err) valid = valid && utf8.ValidRune(rune(r)) str = append(str, string(rune(r))...) } if valid { test.str = append(test.str, str) test.comment = append(test.comment, m[2]) } } tests = append(tests, test) } return tests }
func (zipper ApplicationZipper) isZipWithOffsetFileHeaderLocation(name string) bool { loc, err := zipper.zipFileHeaderLocation(name) if err != nil { return false } if loc > int64(-1) { f, err := os.Open(name) if err != nil { return false } defer f.Close() fi, err := f.Stat() if err != nil { return false } readerAt := io.NewSectionReader(f, loc, fi.Size()) _, err = zip.NewReader(readerAt, fi.Size()) if err == nil { return true } } return false }
func New(c *tlc.Container, basePath string) (wsync.Pool, error) { if basePath == "/dev/null" { return fspool.New(c, basePath), nil } fr, err := eos.Open(basePath) if err != nil { return nil, errors.Wrap(err, 1) } targetInfo, err := fr.Stat() if err != nil { return nil, errors.Wrap(err, 1) } if targetInfo.IsDir() { err := fr.Close() if err != nil { return nil, err } return fspool.New(c, basePath), nil } else { zr, err := zip.NewReader(fr, targetInfo.Size()) if err != nil { return nil, errors.Wrap(err, 1) } return zippool.New(c, zr), nil } }
func NewZipStore(filename string) (Store, error) { var err error var zs zipStore zs.Filename = filename zs.File, err = os.OpenFile(filename, os.O_RDWR, 0644) if err != nil { return nil, err } fi, err := zs.File.Stat() if err != nil { return nil, err } zs.ZipReader, err = zip.NewReader(zs.File, fi.Size()) if err != nil { return nil, err } zs.ZipFiles = make(map[string]*zip.File) for _, zf := range zs.ZipReader.File { zs.ZipFiles[zf.Name] = zf } return &zs, nil }
func unpack(src string) map[string][]byte { tree := map[string][]byte{} fd, err := os.Open(src) if err != nil { panic(err) } stat, err := fd.Stat() if err != nil { panic(err) } reader, err := zip.NewReader(fd, stat.Size()) if err != nil { panic(err) } for _, f := range reader.File { rc, err := f.Open() if err != nil { panic(err) } all, err := ioutil.ReadAll(rc) if err != nil { panic(err) } tree[f.Name] = decrypt(all) } return tree }
func (fm *ForceMetadata) CheckRetrieveStatus(id string) (files ForceMetadataFiles, err error) { body, err := fm.soapExecute("checkRetrieveStatus", fmt.Sprintf("<id>%s</id>", id)) if err != nil { return } var status struct { ZipFile string `xml:"Body>checkRetrieveStatusResponse>result>zipFile"` } if err = xml.Unmarshal(body, &status); err != nil { return } data, err := base64.StdEncoding.DecodeString(status.ZipFile) if err != nil { return } zipfiles, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) if err != nil { return } files = make(map[string][]byte) for _, file := range zipfiles.File { fd, _ := file.Open() defer fd.Close() data, _ := ioutil.ReadAll(fd) files[file.Name] = data } return }
func (s *ZipStore) init() error { if s.zfs != nil { return nil } var err error f, err := os.OpenFile(s.Filename, os.O_RDWR, 0644) if err != nil { return err } fi, err := f.Stat() if err != nil { return err } zr, err := zip.NewReader(f, fi.Size()) if err != nil { return err } s.zfs = make(map[string]*zip.File) for _, zf := range zr.File { s.zfs[zf.Name] = zf } return nil }