func cloneZipItem(f *zip.File, dest string) error {
	// Create full directory path
	path := filepath.Join(dest, f.Name)
	fmt.Println("Creating", path)
	err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm)
	if err != nil {
		return err
	}

	// Clone if item is a file
	rc, err := f.Open()
	if err != nil {
		return err
	}
	if !f.FileInfo().IsDir() {
		// Use os.Create() since Zip don't store file permissions.
		fileCopy, err := os.Create(path)
		if err != nil {
			return err
		}
		_, err = io.Copy(fileCopy, rc)
		fileCopy.Close()
		if err != nil {
			return err
		}
	}
	rc.Close()
	return nil
}
Exemple #2
0
func extractFile(root string, file *zip.File) error {
	fmt.Printf("  extracting %s ...\n", file.Name)
	path := filepath.Join(root, file.Name)
	if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
		return err
	}
	r, err := file.Open()
	if err != nil {
		return err
	}
	perms := file.FileInfo().Mode().Perm()
	f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, perms)
	if err != nil {
		r.Close()
		return err
	}
	if _, err := io.Copy(f, r); err != nil {
		r.Close()
		f.Close()
		return err
	}
	if err := f.Close(); err != nil {
		return err
	}
	return r.Close()
}
Exemple #3
0
func (z *unzipper) unzip(f *zip.File) error {
	if f.FileInfo().IsDir() {
		return nil
	}

	fName := filepath.Join(z.dst, f.Name)
	dir, _ := filepath.Split(fName)

	if err := os.MkdirAll(dir, perm); err != nil && os.IsNotExist(err) {
		return err
	}

	r, err := f.Open()
	if err != nil {
		return err
	}
	defer r.Close()

	w, err := os.Create(filepath.Join(z.dst, f.Name))
	if err != nil {
		return err
	}
	defer w.Close()

	if _, err := io.CopyBuffer(w, r, z.buffer); err != nil {
		w.Close()
		return err
	}

	if err := r.Close(); err != nil {
		return err
	}

	return w.Close()
}
Exemple #4
0
func copyFile(dst string, f *zip.File) (err error) {
	path := filepath.Join(dst, f.Name)
	if err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm); err != nil {
		return fmt.Errorf("mkdir failure:%s", err)
	}

	rc, err := f.Open()
	if err != nil {
		return fmt.Errorf("open failure:%s", err)
	}

	defer rc.Close()

	if !f.FileInfo().IsDir() {
		copied, err := os.Create(path)
		if err != nil {
			return err
		}
		defer copied.Close()

		if _, err := io.Copy(copied, rc); err != nil {
			return fmt.Errorf("copy failure:%s", err)
		}
	}

	return nil
}
Exemple #5
0
func unarchiveFile(zipFile *zip_impl.File, outFilePath string, progress ProgressFunc) error {
	if zipFile.FileInfo().IsDir() {
		return nil
	}

	if progress != nil {
		progress(zipFile.Name)
	}

	zipFileReader, err := zipFile.Open()
	if err != nil {
		return err
	}
	defer func() {
		_ = zipFileReader.Close()
	}()

	filePath := filepath.Join(outFilePath, filepath.Join(strings.Split(zipFile.Name, "/")...))

	err = os.MkdirAll(filepath.Dir(filePath), os.FileMode(0755))
	if err != nil {
		return err
	}

	file, err := os.Create(filePath)
	if err != nil {
		return err
	}
	defer func() {
		_ = file.Close()
	}()

	_, err = io.Copy(file, zipFileReader)
	return err
}
// Writes the contents of a single file contained in webdriver.xpi
func (s *Firefox) writeExtensionFile(f *zip.File) (err error) {

	// seperated this into a function so I could use defer, etc.
	var file io.ReadCloser

	if file, err = f.Open(); err == nil {
		defer file.Close()

		targetFileSpec := filepath.Join(s.Extension.TargetPath, f.Name)
		if f.FileInfo().IsDir() {
			err = os.MkdirAll(targetFileSpec, s.DirPermissions)
		} else {

			var targetFile *os.File
			if targetFile, err = os.OpenFile(targetFileSpec,
				os.O_WRONLY|os.O_CREATE,
				s.FilePermissions); err == nil {

				defer targetFile.Close()
				_, err = io.Copy(targetFile, file)

			}

		}

	}

	return err
}
Exemple #7
0
// extractFile extracts zip.File to file system.
func extractFile(f *zip.File, destPath string) error {
	filePath := path.Join(destPath, f.Name)
	os.MkdirAll(path.Dir(filePath), os.ModePerm)

	rc, err := f.Open()
	if err != nil {
		return err
	}
	defer rc.Close()

	fw, err := os.Create(filePath)
	if err != nil {
		return err
	}
	defer fw.Close()

	if _, err = io.Copy(fw, rc); err != nil {
		return err
	}

	// Skip symbolic links.
	if f.FileInfo().Mode()&os.ModeSymlink != 0 {
		return nil
	}
	// Set back file information.
	if err = os.Chtimes(filePath, f.ModTime(), f.ModTime()); err != nil {
		return err
	}
	return os.Chmod(filePath, f.FileInfo().Mode())
}
func (zipper ApplicationZipper) extractFile(f *zip.File, destDir string) error {
	if f.FileInfo().IsDir() {
		os.MkdirAll(filepath.Join(destDir, f.Name), os.ModeDir|os.ModePerm)
		return nil
	}

	var rc io.ReadCloser
	rc, err := f.Open()
	if err != nil {
		return err
	}

	defer rc.Close()

	destFilePath := filepath.Join(destDir, f.Name)

	err = fileutils.CopyReaderToPath(rc, destFilePath)
	if err != nil {
		return err
	}

	err = os.Chmod(destFilePath, f.FileInfo().Mode())
	if err != nil {
		return err
	}

	return nil
}
Exemple #9
0
func extractZipItem(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 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
}
Exemple #10
0
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
}
Exemple #11
0
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
}
Exemple #12
0
func writeExtensionFile(f *zip.File, extPath string) error {
	weferr := "write extension failed: "
	rc, err := f.Open()
	if err != nil {
		return errors.New(weferr + err.Error())
	}
	defer rc.Close()
	filename := filepath.Join(extPath, f.Name)
	if f.FileInfo().IsDir() {
		err = os.Mkdir(filename, 0770)
		if err != nil {
			return err
		}
	} else {
		dst, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
		if err != nil {
			return errors.New(weferr + err.Error())
		}
		defer dst.Close()
		_, err = io.Copy(dst, rc)
		if err != nil {
			return errors.New(weferr + err.Error())
		}
	}
	return nil
}
Exemple #13
0
func writeExtensionFile(f *zip.File, extPath string) error {
	weferr := "write extension failed: "
	rc, err := f.Open()
	if err != nil {
		return errors.New(weferr + err.Error())
	}
	defer rc.Close()

	filename := filepath.Join(extPath, f.Name)

	// empty files will not create directories, do it manually.
	if f.FileInfo().Size() == 0 {
		os.MkdirAll(filepath.Dir(filename), 0770)
	}

	if f.FileInfo().IsDir() {
		os.MkdirAll(filename, 0770)
	} else {
		dst, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
		if err != nil {
			return errors.New(weferr + err.Error())
		}
		defer dst.Close()

		_, err = io.Copy(dst, rc)
		if err != nil {
			return errors.New(weferr + err.Error())
		}
	}
	return nil
}
Exemple #14
0
// UnpackModpack downloads mods and unpacks file contents.
func UnpackModpack(fname string) {
	z, ze := zip.OpenReader(fname)
	util.Must(ze)
	defer util.MustClose(z)
	info := new(modpackmanifest)
	var zf *zip.File
	ov := "overrides/"
	buf := make([]byte, 32*1024)
	for _, zf = range z.File {
		if zf.Name == "manifest.json" {
			ozf, oze := zf.Open()
			util.Must(oze)
			util.Must(json.NewDecoder(ozf).Decode(info))
			util.MustClose(ozf)
			break
		}
	}
	if info != nil {
		ov = info.Overrides + "/"
		for i, xf := range info.Files {
			pid := strconv.FormatInt(int64(xf.PID), 10)
			pkg := GetPackage(pid)
			if pkg == nil {
				fmt.Printf("Package with ID %s is missing!\n", pid)
				continue
			}
			fmt.Printf("%d / %d ", i, len(info.Files))
			pkg.DownloadFileWithID(strconv.FormatInt(int64(xf.FID), 10), buf)
		}
	}
	lov := len(ov)
	for _, zf = range z.File {
		if len(zf.Name) < lov {
			continue
		}
		n := zf.Name[:lov]
		if n == ov {
			n = zf.Name[lov:]
			if n == "" {
				continue
			}
			if zf.FileInfo().IsDir() {
				util.Must(util.MkDirIfNotExist(n))
			} else {
				xf, xe := zf.Open()
				util.Must(xe)
				fmt.Printf("Unpacking %#v...\n", n)
				pr := util.NewReadProgress(xf, zf.UncompressedSize64)
				f, fe := os.Create(n)
				util.Must(fe)
				_, ce := io.CopyBuffer(f, pr, buf)
				util.Must(ce)
				util.MustClose(f)
				util.MustClose(xf)
				pr.Close()
			}
		}
	}
}
Exemple #15
0
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())
}
Exemple #16
0
func cloneZipItem(f *zip.File, dest string) error {
	// create full directory path
	fileName := f.Name

	if !utf8.ValidString(fileName) {
		data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(fileName)), simplifiedchinese.GB18030.NewDecoder()))
		if nil == err {
			fileName = string(data)
		} else {
			logger.Error(err)
		}
	}

	path := filepath.Join(dest, fileName)

	err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm)
	if nil != err {
		return err
	}

	if f.FileInfo().IsDir() {
		err = os.Mkdir(path, os.ModeDir|os.ModePerm)
		if nil != err {
			return err
		}

		return nil
	}

	// clone if item is a file

	rc, err := f.Open()
	if nil != err {
		return err
	}

	defer rc.Close()

	// use os.Create() since Zip don't store file permissions
	fileCopy, err := os.Create(path)
	if nil != err {
		return err
	}

	defer fileCopy.Close()

	_, err = io.Copy(fileCopy, rc)
	if nil != err {
		return err
	}

	return nil
}
Exemple #17
0
func unzip(zf *zip.File, dir string) error {
	if strings.HasSuffix(zf.Name, "/") {
		return os.MkdirAll(filepath.Join(dir, zf.Name), 0700)
	}

	rc, err := zf.Open()
	if err != nil {
		return fmt.Errorf("%s: open compressed file: %v", zf.Name, err)
	}
	defer rc.Close()

	return writeFile(filepath.Join(dir, zf.Name), rc, zf.FileInfo().Mode())
}
Exemple #18
0
func extractZipFile(path string, f *zip.File) error {
	rc, err := f.Open()
	if err != nil {
		return err
	}
	defer rc.Close()
	file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.FileInfo().Mode())
	if err != nil {
		return err
	}
	defer file.Close()
	_, err = io.Copy(file, rc)
	return err
}
Exemple #19
0
func (u unZip) saveFile(to string, zf *zip.File, s *storage.Storage) (fs int64, err error) {
	if zf.FileInfo().Size() == 0 {
		return
	}
	fname := strings.Trim(to, "/") + "/" + strings.Trim(zf.Name, "/")
	reader, err := zf.Open()
	if err != nil {
		return
	}
	defer reader.Close()
	if f, err := s.Add(fname, reader, zf.FileInfo().Size()); err == nil {
		return f.FSize, nil
	}
	return
}
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
}
Exemple #21
0
func newFile(zf *zip.File, isDir bool) (*file, error) {
	rc, err := zf.Open()
	if err != nil {
		return nil, err
	}
	defer rc.Close()
	all, err := ioutil.ReadAll(rc)
	if err != nil {
		return nil, err
	}
	return &file{
		FileInfo: zf.FileInfo(),
		data:     all,
		readerAt: bytes.NewReader(all),
		Closer:   nopCloser,
		isDir:    isDir,
	}, nil
}
Exemple #22
0
func serveIdentity(w http.ResponseWriter, r *http.Request, zf *zip.File) {
	// TODO: need to check if the client explicitly refuses to accept
	// identity encoding (Accept-Encoding: identity;q=0), but this is
	// going to be very rare.

	reader, err := zf.Open()
	if err != nil {
		internalServerError(w, r, err)
		return
	}
	defer reader.Close()

	size := zf.FileInfo().Size()
	w.Header().Del("Content-Encoding")
	w.Header().Set("Content-Length", fmt.Sprintf("%d", size))
	if r.Method != "HEAD" {
		io.CopyN(w, reader, int64(size))
	}
}
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)
	}
}
Exemple #24
0
func (zipper ApplicationZipper) extractFile(f *zip.File, destDir string) error {
	if f.FileInfo().IsDir() {
		err := os.MkdirAll(filepath.Join(destDir, f.Name), os.ModeDir|os.ModePerm)
		if err != nil {
			return err
		}
		return nil
	}

	src, err := f.Open()
	if err != nil {
		return err
	}
	defer src.Close()

	destFilePath := filepath.Join(destDir, f.Name)

	err = os.MkdirAll(filepath.Dir(destFilePath), os.ModeDir|os.ModePerm)
	if err != nil {
		return err
	}

	destFile, err := os.Create(destFilePath)
	if err != nil {
		return err
	}
	defer destFile.Close()

	_, err = io.Copy(destFile, src)
	if err != nil {
		return err
	}

	err = os.Chmod(destFilePath, f.FileInfo().Mode())
	if err != nil {
		return err
	}

	return nil
}
Exemple #25
0
func ParseAxml(f *zip.File, listener axmlParser.Listener) (*axmlParser.Parser, error) {
	//bs, err := ioutil.ReadFile(axmlpath)
	file, err := f.Open()
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var n int64
	if size := f.FileInfo().Size(); size < 1e9 {
		n = size
	}

	buf := bytes.NewBuffer(make([]byte, 0, n+bytes.MinRead))
	_, err = buf.ReadFrom(file)

	parser := axmlParser.New(listener)
	err = parser.Parse(buf.Bytes())
	if err != nil {
		return nil, err
	}
	return parser, nil
}
Exemple #26
0
func extractFile(f *zip.File, destPath string) error {
	filePath := path.Join(destPath, f.Name)
	// Create diretory before create file
	os.MkdirAll(path.Dir(filePath), os.ModePerm)

	rc, err := f.Open()
	if err != nil {
		return err
	}
	defer rc.Close()

	fw, _ := os.Create(filePath)
	if err != nil {
		return err
	}

	_, err = io.Copy(fw, rc)
	if err != nil {
		return err
	}

	return os.Chmod(filePath, f.FileInfo().Mode())
}
Exemple #27
0
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
}
Exemple #28
0
func extractZipArchiveFile(file *zip.File, dest string, input io.Reader) error {
	filePath := filepath.Join(dest, file.Name)
	fileInfo := file.FileInfo()

	if fileInfo.IsDir() {
		err := os.MkdirAll(filePath, fileInfo.Mode())
		if err != nil {
			return err
		}
	} else {
		err := os.MkdirAll(filepath.Dir(filePath), 0755)
		if err != nil {
			return err
		}

		if fileInfo.Mode()&os.ModeSymlink != 0 {
			linkName, err := ioutil.ReadAll(input)
			if err != nil {
				return err
			}
			return os.Symlink(string(linkName), filePath)
		}

		fileCopy, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileInfo.Mode())
		if err != nil {
			return err
		}
		defer fileCopy.Close()

		_, err = io.Copy(fileCopy, input)
		if err != nil {
			return err
		}
	}

	return nil
}
Exemple #29
0
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
}
Exemple #30
0
func unzipFile(zipFile *zip.File, unzipPath string, w http.ResponseWriter) (os.FileInfo, string, error) {
	destPath := filepath.Join(unzipPath, zipFile.Name)
	destDir, _ := filepath.Split(destPath)
	err := os.MkdirAll(destDir[:len(destDir)-1], 0755)
	if err != nil {
		servutil.Fail(w, "Failed to create destination directory: %s", err)
		return nil, "", err
	}

	if zipFile.FileInfo().IsDir() {
		return zipFile.FileInfo(), "", nil
	}

	dest, err := os.OpenFile(destPath, os.O_CREATE|os.O_RDWR, 0755)
	if err != nil {
		servutil.Fail(w, "Failed to create destination file: %s", err)
		return nil, "", err
	}

	of, err := zipFile.Open()
	if err != nil {
		servutil.Fail(w, "Failed to open application file: %s", err)
		return nil, "", err
	}

	io.Copy(dest, of)
	of.Close()
	fi, err := dest.Stat()
	if err != nil {
		servutil.Fail(w, "Failed to stat application file: %s", err)
		return nil, "", err
	}
	dest.Close()
	fn := dest.Name()
	return fi, fn, nil
}