// ReadZip() takes a pointer to a zip.ReadCloser and returns a
// xlsx.File struct populated with its contents.  In most cases
// ReadZip is not used directly, but is called internally by OpenFile.
352297
func ReadZip(f *zip.ReadCloser) (*File, error) {
	defer f.Close()
	return ReadZipReader(&f.Reader)
}
492675
func Unzip(srcPath, destPath string) error {
	// Open a zip archive for reading
	r, err := zip.OpenReader(srcPath)
	if err != nil {
		return err
	}
	defer r.Close()

	// Iterate through the files in the archive
	for _, f := range r.File {
		// Get files from archive
		rc, err := f.Open()
		if err != nil {
			return err
		}

		dir := filepath.Dir(f.Name)
		// Create directory before create file
		os.MkdirAll(destPath+"/"+dir, os.ModePerm)

		if f.FileInfo().IsDir() {
			continue
		}

		// Write data to file
		fw, _ := os.Create(filepath.Join(destPath, f.Name))
		if err != nil {
			return err
		}
		_, err = io.Copy(fw, rc)

		if fw != nil {
			fw.Close()
		}
		if err != nil {
			return err
		}
	}
	return nil
}
217578
func (f *File) Open() (rc io.ReadCloser, err error) {
	bodyOffset, err := f.findBodyOffset()
	if err != nil {
		return
	}
	size := int64(f.CompressedSize64)
	r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, size)
	dcomp := f.zip.decompressor(f.Method)
	if dcomp == nil {
		err = ErrAlgorithm
		return
	}
	rc = dcomp(r)
	var desr io.Reader
	if f.hasDataDescriptor() {
		desr = io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset+size, dataDescriptorLen)
	}
	rc = &checksumReader{
		rc:   rc,
		hash: crc32.NewIEEE(),
		f:    f,
		desr: desr,
	}
	return
}
83278
func (s *UpdateFunctionCodeInput) SetZipFile(v []byte) *UpdateFunctionCodeInput {
	s.ZipFile = v
	return s
}
359217
func Zipper(zipPath string) (*bytes.Buffer, error) {
	f, err := os.Open(zipPath)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	buffer := bytes.NewBuffer(nil)
	w := zip.NewWriter(buffer)
	fh := zip.FileHeader{}
	fh.Name = filepath.Base(zipPath)
	fh.SetMode(0755)

	// Add some files to the archive.
	var files = []struct {
		Name       string
		FileHandle *os.File
		Header     *zip.FileHeader
	}{
		{fh.Name, f, &fh},
	}

	// Loop through all files listed above
	// Can be created using file.Name or a custom file.Header for flexibility.
	// To set permissions you need to use the file.Header construct.
	for _, file := range files {
		var zf io.Writer
		if file.Header == nil {
			zf, err = w.Create(file.Name)
		} else {
			zf, err = w.CreateHeader(file.Header)
		}
		if err != nil {
			return nil, err
		}

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

	if err := w.Close(); err != nil {
		return nil, err
	}
	return buffer, nil
}
1219594
func Decompress(encoding string, r io.Reader) (io.ReadCloser, error) {
	switch encoding {
	case "gzip":
		return gzip.NewReader(r)
	case "deflate", "zlib":
		// compatible for RFC 1950 zlib, RFC 1951 deflate, http://www.open-open.com/lib/view/open1460866410410.html
		return zlib.NewReader(r)
	default:
		return nil, ErrUnsupportedMediaType.WithMsgf("Unsupported Content-Encoding: %s", encoding)
	}
}
1235147
func Open(filename string) (rc *ReadCloser, err error) {
	fp, err := os.Open(filename)
	if err != nil {
		return nil, err
	}

	if strings.HasSuffix(filename, ".gz") {
		r := &ReadCloser{
			fp:     fp,
			isGzip: true,
			isOpen: true,
		}
		r.gz, err = gzip.NewReader(fp)
		if err != nil {
			fp.Close()
			return nil, err
		}
		r.r = r.gz
		runtime.SetFinalizer(r, (*ReadCloser).Close)
		return r, nil
	}

	if strings.HasSuffix(filename, ".bz2") {
		r := &ReadCloser{
			fp:      fp,
			r:       bzip2.NewReader(fp),
			isBzip2: true,
			isOpen:  true,
		}
		runtime.SetFinalizer(r, (*ReadCloser).Close)
		return r, nil
	}

	r := &ReadCloser{
		fp:      fp,
		r:       fp,
		isOpen:  true,
	}
	runtime.SetFinalizer(r, (*ReadCloser).Close)
	return r, nil
}
1033046
func Extract(src string, dest string) error {
	files, err := zip.OpenReader(src)

	if err != nil {
		return err
	}

	defer files.Close()

	for _, file := range files.File {
		err = extractZip(file, dest)

		if err != nil {
			return err
		}
	}

	return nil
}
115038
func (pr *publishRequest) serveZip() {
	filename := ""
	if len(pr.subres) > len("/=z/") {
		filename = pr.subres[4:]
	}
	zh := &zipHandler{
		fetcher:  pr.ph.cl,
		cl:       pr.ph.cl,
		root:     pr.subject,
		filename: filename,
	}
	zh.ServeHTTP(pr.rw, pr.req)
}
212220
func ReadFile(file string) ([]byte, error) {
	bytes, err := ioutil.ReadFile(file)
	if err != nil {
		return []byte(""), err
	}
	// *100 is an safe guess to ensure the file can be decoded.
	decompressed := make([]byte, len(bytes)*100)
	_, err = lzf.Decompress(bytes, decompressed)
	return decompressed, err
}
502565
func (r *WALReader) readFile(file string, cb func(WALEntry) error) error {
	f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0666)
	if err != nil {
		return err
	}
	defer f.Close()

	stat, err := f.Stat()
	if err != nil {
		return err
	}
	r.logger.Info("Reading file", zap.String("path", file), zap.Int64("size", stat.Size()))

	if stat.Size() == 0 {
		return nil
	}

	if r.r == nil {
		r.r = NewWALSegmentReader(f)
	} else {
		r.r.Reset(f)
	}
	defer r.r.Close()

	for r.r.Next() {
		entry, err := r.r.Read()
		if err != nil {
			n := r.r.Count()
			r.logger.Info("File corrupt", zap.Error(err), zap.String("path", file), zap.Int64("pos", n))
			if err := f.Truncate(n); err != nil {
				return err
			}
			break
		}

		if err := cb(entry); err != nil {
			return err
		}
	}

	return r.r.Close()
}
904521
func (s *UpdateScriptInput) SetZipFile(v []byte) *UpdateScriptInput {
	s.ZipFile = v
	return s
}
1089152
func readFile(fpath string) (io.ReadCloser, error) {
	// Golang strips trailing / if you clean(..) or
	// EvalSymlinks(..). Adding '.' prevents it from doing so.
	if strings.HasSuffix(fpath, "/") {
		fpath = fpath + "."
	}
	fpath, e := filepath.EvalSymlinks(fpath)
	if e != nil {
		return nil, e
	}
	fileData, e := os.Open(fpath)
	if e != nil {
		return nil, e
	}
	return fileData, nil
}
1045673
func (s *FileStorage) readFile(file string) ([]byte, error) {
	b, err := ioutil.ReadFile(file)
	if os.IsNotExist(err) {
		return nil, ErrNotExist(err)
	}
	return b, err
}
126755
func loadZippedExeAssets() (ResourceMap, error) {
	// open ourselves (teleport binary) for reading:
	// NOTE: the file stays open to serve future Read() requests
	myExe, err := osext.Executable()
	if err != nil {
		return nil, trace.Wrap(err)
	}
	return readZipArchive(myExe)
}
931246
func (dh *DownloadHandler) serveZip(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	if r.Method != "POST" {
		http.Error(w, "Invalid download method", http.StatusBadRequest)
		return
	}

	filesValue := r.FormValue("files")
	if filesValue == "" {
		http.Error(w, "No file blobRefs specified", http.StatusBadRequest)
		return
	}
	files := strings.Split(filesValue, ",")

	var refs []blob.Ref
	for _, file := range files {
		br, ok := blob.Parse(file)
		if !ok {
			http.Error(w, fmt.Sprintf("%q is not a valid blobRef", file), http.StatusBadRequest)
			return
		}
		refs = append(refs, br)
	}

	// We check as many things as we can before writing the zip, because
	// once we start sending a response we can't http.Error anymore.
	var allRefs map[blob.Ref]string
	_, ok := (dh.Fetcher).(*cacher.CachingFetcher)
	if ok {
		// If we have a caching fetcher, allRefs and dh.pathByRef are populated with all
		// the input refs plus their children, so we don't have to redo later the recursing
		// work that we're alreading doing in checkFiles.
		dh.pathByRef = make(map[blob.Ref]string, len(refs))
		err := dh.checkFiles(ctx, "", refs)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		allRefs = dh.pathByRef
	} else {
		_, ok := dh.Fetcher.(blobserver.BlobStatter)
		if ok {
			if err := dh.statFiles(refs); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
		// If we don't have a cacher we don't know yet of all the possible
		// children refs, so allRefs is just the input refs, and the
		// children will be discovered on the fly, while building the zip archive.
		// This is the case even if we have a statter, because statFiles does not
		// recurse into directories.
		allRefs = make(map[blob.Ref]string, len(refs))
		for _, v := range refs {
			allRefs[v] = ""
		}
	}

	h := w.Header()
	h.Set("Content-Type", "application/zip")
	zipName := "camli-download-" + time.Now().Format(downloadTimeLayout) + ".zip"
	h.Set("Content-Disposition", "attachment; filename="+zipName)
	zw := zip.NewWriter(w)
	dh.r = r
	for br := range allRefs {
		if err := dh.zipFile(ctx, "", br, zw); err != nil {
			log.Printf("error zipping %v: %v", br, err)
			// http.Error is of no use since we've already started sending a response
			panic(http.ErrAbortHandler)
		}
	}
	if err := zw.Close(); err != nil {
		log.Printf("error closing zip stream: %v", err)
		panic(http.ErrAbortHandler)
	}
}
555601
func (s *CreateScriptInput) SetZipFile(v []byte) *CreateScriptInput {
	s.ZipFile = v
	return s
}