// Unzip unpacks a ZIP stream. When given a os.File reader it will get its size without
// reading the entire zip file in memory.
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)
}
1214134
func (ca *ConnackPacket) Unpack(b io.Reader) error {
	flags, err := decodeByte(b)
	if err != nil {
		return err
	}
	ca.SessionPresent = 1&flags > 0
	ca.ReturnCode, err = decodeByte(b)

	return err
}
714221
func (z *ZipFile) Close() error {
	err := z.Writer.Close()
	// If the out writer supports io.Closer, Close it.
	if c, ok := z.out.(io.Closer); ok {
		c.Close()
	}
	return err
}
1115810
func (pa *PubackPacket) Unpack(b io.Reader) {
	pa.MessageID = decodeUint16(b)
}
294118
func UnmarshalGzipped(data []byte) (Layers, error) {
	gzreader, err := gzip.NewReader(bytes.NewBuffer(data))
	if err != nil {
		return nil, errors.WithMessage(err, "failed to create gzreader")
	}

	decoded, err := ioutil.ReadAll(gzreader)
	if err != nil {
		return nil, errors.WithMessage(err, "failed to unzip")
	}

	return Unmarshal(decoded)
}
498992
func DownloadZip(zipURL string, newDir string, showOutputIndication bool) (string, error) {
	var err error
	var size int64
	if showOutputIndication {
		finish := ShowIndicator(os.Stdout, true)

		defer func() {
			finish <- true
		}()
	}

	os.MkdirAll(newDir, 0755)
	tokens := strings.Split(zipURL, "/")
	fileName := newDir + tokens[len(tokens)-1]
	if !strings.HasSuffix(fileName, ".zip") {
		return "", errNoZip.Format(fileName)
	}

	output, err := os.Create(fileName)
	if err != nil {
		return "", errFileCreate.Format(err.Error())
	}
	defer output.Close()
	response, err := http.Get(zipURL)
	if err != nil {
		return "", errFileDownload.Format(zipURL, err.Error())
	}
	defer response.Body.Close()

	size, err = io.Copy(output, response.Body)
	if err != nil {
		return "", errFileCopy.Format(err.Error())
	}

	if showOutputIndication {
		print("OK ", size, " bytes downloaded")
	}

	return fileName, nil

}
415106
func (pr *PubrecPacket) Unpack(b io.Reader) error {
	var err error
	pr.MessageID, err = decodeUint16(b)

	return err
}
535524
func UnpackMap(in interface{}) (yaml.MapSlice, bool) {
	m, ok := in.(yaml.MapSlice)
	if ok {
		return m, true
	}
	// do we have an empty array?
	a, ok := in.([]interface{})
	if ok && len(a) == 0 {
		// if so, return an empty map
		return yaml.MapSlice{}, true
	}
	return nil, false
}
130842
func (s *SubscribePacket) Unpack(src []byte) {
	s.MessageID = loadUint16(src)
	if len(src) < 2 {
		return // FIXME: error
	}
	src = src[2:]
	for len(src) > 2 {
		topic, end := loadString(src)
		src = src[end:]
		if len(src) < 1 {
			break // FIXME: error
		}
		s.Topics = append(s.Topics, topic)
		qos := loadByte(src)
		src = src[1:]
		s.Qoss = append(s.Qoss, qos)
	}
}
112165
func (p *PackedMulti) Unpack(keyRing keychain.KeyRing) (*Multi, error) {
	var m Multi

	packedReader := bytes.NewReader(*p)
	if err := m.UnpackFromReader(packedReader, keyRing); err != nil {
		return nil, err
	}

	return &m, nil
}
636261
func Gzip() fibre.MiddlewareFunc {
	return func(h fibre.HandlerFunc) fibre.HandlerFunc {
		return func(c *fibre.Context) error {

			// This is a socket
			if c.IsSocket() {
				return h(c)
			}

			// Set the accept-encoding header

			c.Response().Header().Add(fibre.HeaderVary, "Accept-Encoding")

			// Check to see if the client can accept gzip encoding

			if strings.Contains(c.Request().Header().Get(fibre.HeaderAcceptEncoding), "gzip") {

				z := &zipper{ResponseWriter: c.Response().Writer()}

				c.Response().SetWriter(z)

				defer z.Close()

			}

			return h(c)

		}
	}
}
832526
func (z *Zip) Open(in io.Reader, size int64) error {
	inRdrAt, ok := in.(io.ReaderAt)
	if !ok {
		return fmt.Errorf("reader must be io.ReaderAt")
	}
	if z.zr != nil {
		return fmt.Errorf("zip archive is already open for reading")
	}
	var err error
	z.zr, err = zip.NewReader(inRdrAt, size)
	if err != nil {
		return fmt.Errorf("creating reader: %v", err)
	}
	z.ridx = 0
	return nil
}
781242
func NewZip(dest io.Writer) *Zip {
	writer := zip.NewWriter(dest)

	writer.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
		return flate.NewWriter(out, flate.DefaultCompression)
	})

	return &Zip{writer: writer}
}
978638
func Unpack(reader io.ReadSeeker, endianness Endianness, data interface{}, offsets ...int64) error {

	switch {
	// No offset to deal with
	case len(offsets) == 0:
		if err := binary.Read(reader, endianness, data); err != nil {
			return err
		}
		// An offset to deal with
	case len(offsets) == 1:
		if soughtOffset, err := reader.Seek(offsets[0], os.SEEK_SET); soughtOffset != offsets[0] || err != nil {
			switch {
			case err != nil:
				return err
			case soughtOffset != offsets[0]:
				return ErrSeeking
			default:
				if err := binary.Read(reader, endianness, data); err != nil {
					return err
				}
			}
		}
		// Error if more than one offset
	default:
		return ErrMultipleOffsets
	}
	return nil
}
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
}
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
}
497859
func (z *Zip) Close() error {
	if z.zr != nil {
		z.zr = nil
	}
	if z.zw != nil {
		zw := z.zw
		z.zw = nil
		return zw.Close()
	}
	return nil
}