// Gzipper gzips the file at the path. Helper to prepare gzipped data for upload
// to S3.
771251
func Gzipper(gzipPath string) (*bytes.Buffer, error) {
	f, err := os.Open(gzipPath)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	// Gzip the binary data and toss it in a buffer.
	buffer := bytes.NewBuffer(nil)
	gzipData := gzip.NewWriter(buffer)
	gzipData.Header.Name = filepath.Base(gzipPath)
	gzipData.Header.ModTime = time.Now()
	if _, err := io.Copy(gzipData, f); err != nil {
		return nil, err
	}
	if err := gzipData.Close(); err != nil {
		return nil, err
	}
	return buffer, nil
}
1051397
func CompressWithGzip(data io.Reader) (io.Reader, error) {
	pipeReader, pipeWriter := io.Pipe()
	gzipWriter := gzip.NewWriter(pipeWriter)

	var err error
	go func() {
		_, err = io.Copy(gzipWriter, data)
		gzipWriter.Close()
		// subsequent reads from the read half of the pipe will
		// return no bytes and the error err, or EOF if err is nil.
		pipeWriter.CloseWithError(err)
	}()

	return pipeReader, err
}
312384
func (gz *Gzip) Compress(content []byte) ([]byte, error) {
	if !gz.Options.Compress {
		return content, nil
	}

	// method
	var m int
	switch gz.Options.Method {
	case "NoCompression":
		m = flate.NoCompression
		break
	case "BestSpeed":
		m = flate.BestSpeed
		break
	case "BestCompression":
		m = flate.BestCompression
		break
	default:
		m = flate.DefaultCompression
		break
	}

	// compress
	var b bytes.Buffer
	w, err := gzip.NewWriterLevel(&b, m)
	if err != nil {
		return nil, err
	}

	// insert content
	_, err = w.Write(content)
	if err != nil {
		return nil, err
	}

	err = w.Close()
	if err != nil {
		return nil, err
	}

	// compressed content
	return b.Bytes(), nil
}
560834
func GzipAssetNames() []string {
	names := make([]string, 0, len(_gzipbindata))
	for name := range _gzipbindata {
		names = append(names, name)
	}
	return names
}
1023088
func UnzipPath(sourcePath string, log Log) (string, error) {
	unzipPath := fmt.Sprintf("%s.unzipped", sourcePath)
	err := unzipOver(sourcePath, unzipPath, log)
	if err != nil {
		return "", err
	}
	return unzipPath, nil
}
1041574
func NewDownloadZipResult(Metadata *FolderMetadata) *DownloadZipResult {
	s := new(DownloadZipResult)
	s.Metadata = Metadata
	return s
}
337019
func Unzip(r io.Reader, destPath string) (string, error) {
	var (
		zr  *zip.Reader
		err error
	)

	if f, ok := r.(*os.File); ok {
		fstat, err := f.Stat()
		if err != nil {
			return "", err
		}
		zr, err = zip.NewReader(f, fstat.Size())
	} else {
		data, err := ioutil.ReadAll(r)
		if err != nil {
			return "", err
		}
		memReader := bytes.NewReader(data)
		zr, err = zip.NewReader(memReader, memReader.Size())
	}

	if err != nil {
		return "", err
	}

	return unpackZip(zr, destPath)
}
305254
func GZIPHandler(w http.ResponseWriter, r *http.Request) {
	h, _, _ := net.SplitHostPort(r.RemoteAddr)

	v := gzipResponse{
		headersResponse: headersResponse{getHeaders(r)},
		ipResponse:      ipResponse{h},
		Gzipped:         true,
	}

	w.Header().Set("Content-Type", "application/json")
	w.Header().Add("Content-Encoding", "gzip")
	ww := gzip.NewWriter(w)
	defer ww.Close() // flush
	if err := writeJSON(ww, v); err != nil {
		writeErrorJSON(w, errors.Wrap(err, "failed to write json"))
	}
}
823165
func (z *ReadZipper) GzipHandler(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var err error
		if "gzip" == r.Header.Get("Content-Encoding") {
			gzi := z.zippers.Get()
			if gzi != nil {
				gz := gzi.(*gzip.Reader)
				// put it back
				defer z.zippers.Put(gz)
				err = gz.Reset(r.Body)
				if err == nil {
					defer log.IfErr(z.Log, gz.Close())
					// nasty? could construct another object but seems expensive
					r.Body = gz
					atomic.AddInt64(&z.HitCount, 1)
					h.ServeHTTP(w, r)
					return
				}
			}
		}
		if err != nil {
			atomic.AddInt64(&z.ErrCount, 1)
			w.WriteHeader(http.StatusBadRequest)
			_, err := w.Write([]byte("error handling gzip compressed request " + err.Error()))
			log.IfErr(z.Log, err)
			return
		}
		atomic.AddInt64(&z.MissCount, 1)
		h.ServeHTTP(w, r)
	})
}
626415
func GzipHandler(h http.Handler, webDir string) http.Handler {
	return &gzipHandler{
		Handler: h,
		webDir:  webDir,
	}
}
82534
func GzipAsset(name string) ([]byte, error) {
	cannonicalName := strings.Replace(name, "\\", "/", -1)
	if f, ok := _gzipbindata[cannonicalName]; ok {
		a, err := f()
		if err != nil {
			return nil, fmt.Errorf("GzipAsset %s can't read by error: %v", name, err)
		}
		return a.bytes, nil
	}
	return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
}
738416
func Unzip(ctx context.Context, src, dst string) (err error) {
	// Clean up on error
	defer func(err *error) {
		if *err != nil || ctx.Err() != nil {
			astilog.Debugf("Removing %s...", dst)
			os.RemoveAll(dst)
		}
	}(&err)

	// Unzipping
	astilog.Debugf("Unzipping %s into %s", src, dst)
	if err = astiarchive.Unzip(ctx, src, dst); err != nil {
		err = errors.Wrapf(err, "unzipping %s into %s failed", src, dst)
		return
	}
	return
}
904521
func (s *UpdateScriptInput) SetZipFile(v []byte) *UpdateScriptInput {
	s.ZipFile = v
	return s
}
1365020
func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
	if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
		return g.Next.ServeHTTP(w, r)
	}
outer:
	for _, c := range g.Configs {

		// Check request filters to determine if gzipping is permitted for this request
		for _, filter := range c.RequestFilters {
			if !filter.ShouldCompress(r) {
				continue outer
			}
		}

		// In order to avoid unused memory allocation, gzip.putWriter only be called when gzip compression happened.
		// see https://github.com/mholt/caddy/issues/2395
		gz := &gzipResponseWriter{
			ResponseWriterWrapper: &httpserver.ResponseWriterWrapper{ResponseWriter: w},
			newWriter: func() io.Writer {
				// gzipWriter modifies underlying writer at init,
				// use a discard writer instead to leave ResponseWriter in
				// original form.
				return getWriter(c.Level)
			},
		}

		defer func() {
			if gzWriter, ok := gz.internalWriter.(*gzip.Writer); ok {
				putWriter(c.Level, gzWriter)
			}
		}()

		var rw http.ResponseWriter
		// if no response filter is used
		if len(c.ResponseFilters) == 0 {
			// replace discard writer with ResponseWriter
			if gzWriter, ok := gz.Writer().(*gzip.Writer); ok {
				gzWriter.Reset(w)
			}
			rw = gz
		} else {
			// wrap gzip writer with ResponseFilterWriter
			rw = NewResponseFilterWriter(c.ResponseFilters, gz)
		}

		// Any response in forward middleware will now be compressed
		status, err := g.Next.ServeHTTP(rw, r)

		// If there was an error that remained unhandled, we need
		// to send something back before gzipWriter gets closed at
		// the return of this method!
		if status >= 400 {
			httpserver.DefaultErrorFunc(w, r, status)
			return 0, err
		}
		return status, err
	}

	// no matching filter
	return g.Next.ServeHTTP(w, r)
}
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
}
383812
func GzipWalker(file string, tmp io.Writer) error {
	f, err := os.Open(file)

	if err != nil {
		return err
	}

	defer f.Close()

	//gzipper
	gz := gzip.NewWriter(tmp)
	defer gz.Close()

	_, err = io.Copy(gz, f)

	return err
}
133516
func Unzip(ctx context.Context, src, dst string) (err error) {
	// Parse src path
	var split = strings.Split(src, ".zip")
	src = split[0] + ".zip"
	var internalPath string
	if len(split) >= 2 {
		internalPath = split[1]
	}

	// Open overall reader
	var r *zip.ReadCloser
	if r, err = zip.OpenReader(src); err != nil {
		return errors.Wrapf(err, "astiarchive: opening overall zip reader on %s failed", src)
	}
	defer r.Close()

	// Loop through files to determine their type
	var dirs, files, symlinks = make(map[string]*zip.File), make(map[string]*zip.File), make(map[string]*zip.File)
	for _, f := range r.File {
		// Validate internal path
		var n = string(os.PathSeparator) + f.Name
		if internalPath != "" && !strings.HasPrefix(n, internalPath) {
			continue
		}
		var p = filepath.Join(dst, strings.TrimPrefix(n, internalPath))

		// Check file type
		if f.FileInfo().Mode()&os.ModeSymlink != 0 {
			symlinks[p] = f
		} else if f.FileInfo().IsDir() {
			dirs[p] = f
		} else {
			files[p] = f
		}
	}

	// Create dirs
	for p, f := range dirs {
		if err = os.MkdirAll(p, f.FileInfo().Mode().Perm()); err != nil {
			return errors.Wrapf(err, "astiarchive: mkdirall %s failed", p)
		}
	}

	// Create files
	for p, f := range files {
		if err = createZipFile(ctx, f, p); err != nil {
			return errors.Wrapf(err, "astiarchive: creating zip file into %s failed", p)
		}
	}

	// Create symlinks
	for p, f := range symlinks {
		if err = createZipSymlink(f, p); err != nil {
			return errors.Wrapf(err, "astiarchive: creating zip symlink into %s failed", p)
		}
	}
	return
}