示例#1
0
文件: gzip.go 项目: glycerine/pgzip
// Step 1: compresses buffer to buffer
// Step 2: send writer to channel
// Step 3: Close result channel to indicate we are done
func compressBlock(p, prevTail []byte, z Writer, r result) {
	defer close(r.result)
	buf := make([]byte, 0, len(p))
	dest := bytes.NewBuffer(buf)

	var compressor *flate.Writer
	var err error
	if len(prevTail) > 0 {
		compressor, err = flate.NewWriterDict(dest, z.level, prevTail)
	} else {
		compressor, err = flate.NewWriter(dest, z.level)
	}
	if err != nil {
		z.pushError(err)
		return
	}
	compressor.Write(p)

	err = compressor.Flush()
	if err != nil {
		z.pushError(err)
		return
	}
	if z.closed {
		err = compressor.Close()
		if err != nil {
			z.pushError(err)
			return
		}
	}
	// Read back buffer
	buf = dest.Bytes()
	r.result <- buf
}
示例#2
0
func ExampleWriter_RegisterCompressor() {
	// Override the default Deflate compressor with a higher compression
	// level.

	// Create a buffer to write our archive to.
	buf := new(bytes.Buffer)

	// Create a new zip archive.
	w := zip.NewWriter(buf)

	var fw *flate.Writer

	// Register the deflator.
	w.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
		var err error
		if fw == nil {
			// Creating a flate compressor for every file is
			// expensive, create one and reuse it.
			fw, err = flate.NewWriter(out, flate.BestCompression)
		} else {
			fw.Reset(out)
		}
		return fw, err
	})

	// Proceed to add files to w.
}
示例#3
0
func newFlateWriter(w io.Writer) io.WriteCloser {
	fw, ok := flateWriterPool.Get().(*flate.Writer)
	if ok {
		fw.Reset(w)
	} else {
		fw, _ = flate.NewWriter(w, 5)
	}
	return &pooledFlateWriter{fw: fw}
}
示例#4
0
func init() {
	registerEncoder(FormatFlate, "kp",
		func(w io.Writer, lvl int) io.WriteCloser {
			zw, err := flate.NewWriter(w, lvl)
			if err != nil {
				panic(err)
			}
			return zw
		})
	registerDecoder(FormatFlate, "kp",
		func(r io.Reader) io.ReadCloser {
			return flate.NewReader(r)
		})
}
示例#5
0
func (_ Test) GZip() {

	var in bytes.Buffer

	w, _ := gzip.NewWriterLevel(&in, gzip.BestSpeed)
	w.Write(testData)
	w.Close()
	out := in.Bytes()
	e.InfoLog.Println("Result of Gzip compression", len(out))

	var in2 bytes.Buffer

	f, _ := flate.NewWriter(&in2, flate.BestSpeed)
	f.Write(testData)
	f.Close()
	out = in2.Bytes()
	e.InfoLog.Println("Result of Flate compression", len(out))
}
示例#6
0
文件: gzip.go 项目: ChongFeng/beats
// Write writes a compressed form of p to the underlying io.Writer. The
// compressed bytes are not necessarily flushed until the Writer is closed.
func (z *Writer) Write(p []byte) (int, error) {
	if z.err != nil {
		return 0, z.err
	}
	var n int
	// Write the GZIP header lazily.
	if !z.wroteHeader {
		z.wroteHeader = true
		z.buf[0] = gzipID1
		z.buf[1] = gzipID2
		z.buf[2] = gzipDeflate
		z.buf[3] = 0
		if z.Extra != nil {
			z.buf[3] |= 0x04
		}
		if z.Name != "" {
			z.buf[3] |= 0x08
		}
		if z.Comment != "" {
			z.buf[3] |= 0x10
		}
		put4(z.buf[4:8], uint32(z.ModTime.Unix()))
		if z.level == BestCompression {
			z.buf[8] = 2
		} else if z.level == BestSpeed {
			z.buf[8] = 4
		} else {
			z.buf[8] = 0
		}
		z.buf[9] = z.OS
		n, z.err = z.w.Write(z.buf[0:10])
		if z.err != nil {
			return n, z.err
		}
		if z.Extra != nil {
			z.err = z.writeBytes(z.Extra)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.Name != "" {
			z.err = z.writeString(z.Name)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.Comment != "" {
			z.err = z.writeString(z.Comment)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.compressor == nil {
			z.compressor, _ = flate.NewWriter(z.w, z.level)
		}
	}
	z.size += uint32(len(p))
	z.digest.Write(p)
	n, z.err = z.compressor.Write(p)
	return n, z.err
}
示例#7
0
	"github.com/corestoreio/csfw/net/ctxhttp"
	"github.com/corestoreio/csfw/net/httputil"
	"github.com/klauspost/compress/flate"
	"github.com/klauspost/compress/gzip"
	"golang.org/x/net/context"
)

var gzWriterPool = sync.Pool{
	New: func() interface{} {
		return gzip.NewWriter(ioutil.Discard)
	},
}

var defWriterPool = sync.Pool{
	New: func() interface{} {
		w, err := flate.NewWriter(ioutil.Discard, 2)
		if err != nil {
			panic(err)
		}
		return w
	},
}

type compressWriter struct {
	io.Writer
	http.ResponseWriter
}

func (w compressWriter) Header() http.Header {
	return w.ResponseWriter.Header()
}