func unzipFile(f *zip.File, dest string) error { path := filepath.Join(dest, f.Name) if f.FileInfo().IsDir() { return os.MkdirAll(path, f.Mode()) } if !FileExists(filepath.Dir(path)) { err := os.MkdirAll(filepath.Dir(path), 0755) if err != nil { return err } } fc, err := f.Open() if err != nil { return err } defer fc.Close() path = MaybeEscapeWinPath(path) dst, err := os.Create(path) if err != nil { return err } defer dst.Close() _, err = io.Copy(dst, fc) return err }
func zipAttr(f *zip.File, a *fuse.Attr) { a.Size = f.UncompressedSize64 a.Mode = f.Mode() a.Mtime = f.ModTime() a.Ctime = f.ModTime() a.Crtime = f.ModTime() }
func extractFile(f *zip.File, dest, md5 string) error { rc, err := f.Open() if err != nil { return err } defer func() { if err := rc.Close(); err != nil { panic(err) } }() path := filepath.Join(dest, md5+".dex") fout, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { return err } defer func() { if err := fout.Close(); err != nil { panic(err) } }() _, err = io.Copy(fout, rc) if err != nil { return err } return nil }
func extractAndWriteFile(f *zip.File, dest string) error { rc, err := f.Open() if err != nil { return err } defer rc.Close() path := filepath.Join(dest, f.Name) if isDir(f.Name) { 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 }
// expand unpacks a charm's zip file into the given directory. // The hooks map holds all the possible hook names in the // charm. func (b *Bundle) expand(hooks map[string]bool, dir string, zfile *zip.File) error { cleanName := filepath.Clean(zfile.Name) if cleanName == "revision" { return nil } r, err := zfile.Open() if err != nil { return err } defer r.Close() mode := zfile.Mode() path := filepath.Join(dir, cleanName) if strings.HasSuffix(zfile.Name, "/") || mode&os.ModeDir != 0 { err = os.MkdirAll(path, mode&0777) if err != nil { return err } return nil } base, _ := filepath.Split(path) err = os.MkdirAll(base, 0755) if err != nil { return err } if mode&os.ModeSymlink != 0 { data, err := ioutil.ReadAll(r) if err != nil { return err } target := string(data) if err := checkSymlinkTarget(dir, cleanName, target); err != nil { return err } return os.Symlink(target, path) } if filepath.Dir(cleanName) == "hooks" { hookName := filepath.Base(cleanName) if _, ok := hooks[hookName]; mode&os.ModeType == 0 && ok { // Set all hooks executable (by owner) mode = mode | 0100 } } if err := checkFileType(cleanName, mode); err != nil { return err } f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode&0777) if err != nil { return err } _, err = io.Copy(f, r) f.Close() return err }
func extractZipDirectoryEntry(file *zip.File) (err error) { err = os.Mkdir(file.Name, file.Mode().Perm()) // The error that directory does exists is not a error for us if os.IsExist(err) { err = nil } return }
func (z *Zip) witeFile(name string, file *zip.File) error { if info := file.FileInfo(); info.IsDir() { return nil } rc, err := file.Open() if err != nil { return err } defer rc.Close() return writeFile(name, rc, file.Mode()) }
func (b *Bundle) expand(dir string, zfile *zip.File) error { cleanName := filepath.Clean(zfile.Name) if cleanName == "revision" { return nil } r, err := zfile.Open() if err != nil { return err } defer r.Close() mode := zfile.Mode() path := filepath.Join(dir, cleanName) if strings.HasSuffix(zfile.Name, "/") || mode&os.ModeDir != 0 { err = os.MkdirAll(path, mode&0777) if err != nil { return err } return nil } base, _ := filepath.Split(path) err = os.MkdirAll(base, 0755) if err != nil { return err } if mode&os.ModeSymlink != 0 { data, err := ioutil.ReadAll(r) if err != nil { return err } target := string(data) if err := checkSymlinkTarget(dir, cleanName, target); err != nil { return err } return os.Symlink(target, path) } if err := checkFileType(cleanName, mode); err != nil { return err } f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode&0777) if err != nil { return err } _, err = io.Copy(f, r) f.Close() return err }
func extractAndWriteFile(i int, f *zip.File) error { var isDir bool rc, err := f.Open() if err != nil { fmt.Println("Error : ", err) return err } defer func() { if err := rc.Close(); err != nil { fmt.Println("Error : ", err) return } }() path := filepath.Join(unzipDest, f.Name) if f.FileInfo().IsDir() { os.MkdirAll(path, f.Mode()) isDir = true } else { f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { fmt.Println("Error : ", err) return err } defer func() { if err := f.Close(); err != nil { fmt.Println("Error : ", err) return } }() _, err = io.Copy(f, rc) if err != nil { fmt.Println("Error : ", err) return err } } if i == 0 { basePath = path } createTree(i+1, path, isDir) return nil }
func unzipFile(f *zip.File, destPath string) error { pathSeparator := string(os.PathSeparator) rc, err := f.Open() if err != nil { return err } defer func() { if err := rc.Close(); err != nil { log.Println(err) } }() filePath := sanitize(f.Name) sepInd := strings.LastIndex(filePath, pathSeparator) // If the file is a subdirectory, it creates it before attempting to // create the actual file if sepInd > -1 { directory := filePath[0:sepInd] if err := os.MkdirAll(filepath.Join(destPath, directory), 0740); err != nil { return err } } file, err := os.Create(filepath.Join(destPath, filePath)) if err != nil { return err } defer func() { if err := file.Close(); err != nil { log.Println(err) } }() file.Chmod(f.Mode()) os.Chtimes(file.Name(), time.Now(), f.ModTime()) if _, err := io.CopyN(file, rc, int64(f.UncompressedSize64)); err != nil { return err } return nil }
func extractZipFileEntry(file *zip.File) (err error) { var out *os.File in, err := file.Open() if err != nil { return err } defer in.Close() // Remove file before creating a new one, otherwise we can error that file does exist os.Remove(file.Name) out, err = os.OpenFile(file.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode().Perm()) if err != nil { return err } defer out.Close() _, err = io.Copy(out, in) return }
func extractZipFile(file *zip.File) (err error) { // Create all parents to extract the file os.MkdirAll(filepath.Dir(file.Name), 0777) switch file.Mode() & os.ModeType { case os.ModeDir: err = extractZipDirectoryEntry(file) case os.ModeSymlink: err = extractZipSymlinkEntry(file) case os.ModeNamedPipe, os.ModeSocket, os.ModeDevice: // Ignore the files that of these types logrus.Warningln("File ignored: %q", file.Name) default: err = extractZipFileEntry(file) } return }
func execEmbeddedBinary(tmpDir string, f *zip.File) ([]byte, error) { tmpName := filepath.Join(tmpDir, f.Name) rc, err := f.Open() if err != nil { return nil, err } defer rc.Close() tmpFile, err := os.OpenFile(tmpName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { return nil, err } _, err = io.Copy(tmpFile, rc) if err != nil { return nil, err } err = tmpFile.Close() if err != nil { return nil, err } return exec.Command(tmpName).CombinedOutput() }
func (x extractor) extract(zipFile *zip.File) error { targetPath, ok := x.targetPath(zipFile) if !ok { return nil } parentPath := filepath.Dir(targetPath) if err := os.MkdirAll(parentPath, 0777); err != nil { return err } mode := zipFile.Mode() modePerm := mode & os.ModePerm modeType := mode & os.ModeType switch modeType { case os.ModeDir: return x.writeDir(targetPath, modePerm) case os.ModeSymlink: return x.writeSymlink(targetPath, zipFile) case 0: return x.writeFile(targetPath, zipFile, modePerm) } return fmt.Errorf("unknown file type %d", modeType) }
func createFile(v *zip.File, dscDir string) error { v.Name = dscDir + v.Name info := v.FileInfo() if info.IsDir() { err := os.MkdirAll(v.Name, v.Mode()) if err != nil { return errors.New(fmt.Sprintf("Error Create direcotry %s faild:\n%s\n", v.Name, err)) } return nil } srcFile, err := v.Open() if err != nil { return errors.New(fmt.Sprintf("Error Read from zip faild:\n%s\n", err)) } defer srcFile.Close() newFile, err := os.Create(v.Name) if err != nil { return errors.New(fmt.Sprintf("Error Create file faild:\n%s\n", err)) } defer newFile.Close() io.Copy(newFile, srcFile) return nil }
func extractFile(f *zip.File, dest string) error { frc, err := f.Open() if err != nil { return err } defer frc.Close() path := filepath.Join(dest, f.Name) if f.FileInfo().IsDir() { os.MkdirAll(path, f.Mode()) } else { f, err := os.Create(path) if err != nil { return err } defer f.Close() _, err = io.Copy(f, frc) if err != nil { return err } } return nil }
func SaveZipEntryToDisk(logger SimpleLogger, destinationFolder string, fileEntry *zip.File) { rc, err := fileEntry.Open() CheckError(err) defer rc.Close() path := filepath.Join(destinationFolder, fileEntry.Name) if fileEntry.FileInfo().IsDir() { os.MkdirAll(path, fileEntry.Mode()) } else { os.MkdirAll(filepath.Dir(path), fileEntry.Mode()) file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileEntry.Mode()) CheckError(err) defer file.Close() _, err = io.Copy(file, rc) CheckError(err) } }
// Función encargada de copiar un directorio func copyDirectory(path string, dir *zip.File) (string, error) { return createDirectory(path+dir.Name, dir.Mode()) }
func (s *DirSuite) assertBundleTo(c *gc.C, baseDir, charmDir string) { var haveSymlinks = true if err := os.Symlink("../target", filepath.Join(charmDir, "hooks/symlink")); err != nil { haveSymlinks = false } dir, err := charm.ReadDir(charmDir) c.Assert(err, gc.IsNil) path := filepath.Join(baseDir, "bundle.charm") file, err := os.Create(path) c.Assert(err, gc.IsNil) err = dir.BundleTo(file) file.Close() c.Assert(err, gc.IsNil) zipr, err := zip.OpenReader(path) c.Assert(err, gc.IsNil) defer zipr.Close() var metaf, instf, emptyf, revf, symf *zip.File for _, f := range zipr.File { c.Logf("Bundled file: %s", f.Name) switch f.Name { case "revision": revf = f case "metadata.yaml": metaf = f case "hooks/install": instf = f case "hooks/symlink": symf = f case "empty/": emptyf = f case "build/ignored": c.Errorf("bundle includes build/*: %s", f.Name) case ".ignored", ".dir/ignored": c.Errorf("bundle includes .* entries: %s", f.Name) } } c.Assert(revf, gc.NotNil) reader, err := revf.Open() c.Assert(err, gc.IsNil) data, err := ioutil.ReadAll(reader) reader.Close() c.Assert(err, gc.IsNil) c.Assert(string(data), gc.Equals, "1") c.Assert(metaf, gc.NotNil) reader, err = metaf.Open() c.Assert(err, gc.IsNil) meta, err := charm.ReadMeta(reader) reader.Close() c.Assert(err, gc.IsNil) c.Assert(meta.Name, gc.Equals, "dummy") c.Assert(instf, gc.NotNil) // Despite it being 0751, we pack and unpack it as 0755. c.Assert(instf.Mode()&0777, gc.Equals, os.FileMode(0755)) if haveSymlinks { c.Assert(symf, gc.NotNil) c.Assert(symf.Mode()&0777, gc.Equals, os.FileMode(0777)) reader, err = symf.Open() c.Assert(err, gc.IsNil) data, err = ioutil.ReadAll(reader) reader.Close() c.Assert(err, gc.IsNil) c.Assert(string(data), gc.Equals, "../target") } else { c.Assert(symf, gc.IsNil) } c.Assert(emptyf, gc.NotNil) c.Assert(emptyf.Mode()&os.ModeType, gc.Equals, os.ModeDir) // Despite it being 0750, we pack and unpack it as 0755. c.Assert(emptyf.Mode()&0777, gc.Equals, os.FileMode(0755)) }
func (s *DirSuite) TestBundleTo(c *C) { dir := testing.Charms.Dir("series", "dummy") path := filepath.Join(c.MkDir(), "bundle.charm") file, err := os.Create(path) c.Assert(err, IsNil) err = dir.BundleTo(file) file.Close() c.Assert(err, IsNil) zipr, err := zip.OpenReader(path) c.Assert(err, IsNil) defer zipr.Close() var metaf, instf, emptyf, revf, symf *zip.File for _, f := range zipr.File { c.Logf("Bundled file: %s", f.Name) switch f.Name { case "revision": revf = f case "metadata.yaml": metaf = f case "hooks/install": instf = f case "hooks/symlink": symf = f case "empty/": emptyf = f case "build/ignored": c.Errorf("bundle includes build/*: %s", f.Name) case ".ignored", ".dir/ignored": c.Errorf("bundle includes .* entries: %s", f.Name) } } c.Assert(revf, NotNil) reader, err := revf.Open() c.Assert(err, IsNil) data, err := ioutil.ReadAll(reader) reader.Close() c.Assert(err, IsNil) c.Assert(string(data), Equals, "1") c.Assert(metaf, NotNil) reader, err = metaf.Open() c.Assert(err, IsNil) meta, err := charm.ReadMeta(reader) reader.Close() c.Assert(err, IsNil) c.Assert(meta.Name, Equals, "dummy") c.Assert(instf, NotNil) // Despite it being 0751, we pack and unpack it as 0755. c.Assert(instf.Mode()&0777, Equals, os.FileMode(0755)) c.Assert(symf, NotNil) c.Assert(symf.Mode()&0777, Equals, os.FileMode(0777)) reader, err = symf.Open() c.Assert(err, IsNil) data, err = ioutil.ReadAll(reader) reader.Close() c.Assert(err, IsNil) c.Assert(string(data), Equals, "../target") c.Assert(emptyf, NotNil) c.Assert(emptyf.Mode()&os.ModeType, Equals, os.ModeDir) // Despite it being 0750, we pack and unpack it as 0755. c.Assert(emptyf.Mode()&0777, Equals, os.FileMode(0755)) }