本文整理匯總了Golang中compress/gzip.NewWriterLevel函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewWriterLevel函數的具體用法?Golang NewWriterLevel怎麽用?Golang NewWriterLevel使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewWriterLevel函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewGzipResponseWriterLevelFile
func NewGzipResponseWriterLevelFile(w http.ResponseWriter, r *http.Request, level int, file *os.File) *GzipResponseWriter {
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
if level < gzip.DefaultCompression || level > gzip.BestCompression {
level = gzip.DefaultCompression
}
var gz *gzip.Writer
if file != nil {
gz, _ = gzip.NewWriterLevel(io.MultiWriter(w, file), level)
} else {
gz, _ = gzip.NewWriterLevel(w, level)
}
resp := &GzipResponseWriter{
ResponseWriter: w,
gzip: gz,
}
header := w.Header()
header.Set("Content-Encoding", "gzip")
if vary, exists := header["Vary"]; !exists || !validate.IsIn("Accept-Encoding", vary...) {
header.Add("Vary", "Accept-Encoding")
}
return resp
}
return &GzipResponseWriter{w, nil}
}
示例2: ServeJSONEncode
//ServeJSONEncode is eager JSON writer
// with gzip encoding where possible
func ServeJSONEncode(w http.ResponseWriter, r *http.Request, v interface{}) {
w.Header().Set("Content-Type", applicationJson)
var pipe AcceptEncoding
// Accept-Encoding has gzip
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
pipe = Gzip
} else {
pipe = PassThrough
}
switch pipe {
case PassThrough:
err := json.NewEncoder(w).Encode(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
case Gzip:
w.Header().Set("Content-Encoding", "gzip")
gz, err := gzip.NewWriterLevel(w, flate.DefaultCompression)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer gz.Close()
json.NewEncoder(gz).Encode(v)
}
return
}
示例3: writer
func (a *Archive) writer() (*tharWriter, error) {
writer := io.Writer(a.Stream)
flushers := []flushableWriter{}
closers := []closeableWriter{}
if a.Options.GZip {
if a.Options.GZipLevel > 0 {
gw, err := gzip.NewWriterLevel(writer, a.Options.GZipLevel)
if err != nil {
return nil, err
}
flushers = append([]flushableWriter{gw}, flushers...)
closers = append([]closeableWriter{gw}, closers...)
writer = gw
} else {
writer = gzip.NewWriter(writer)
}
}
tw := tar.NewWriter(writer)
flushers = append([]flushableWriter{tw}, flushers...)
return &tharWriter{
Writer: tw,
Flushers: flushers,
Closers: closers,
}, nil
}
示例4: shareFiles
// shareFiles writes the metadata of each file specified by nicknames to w.
// This output can be shared with other daemons, giving them access to those
// files.
func (r *Renter) shareFiles(nicknames []string, w io.Writer) error {
if len(nicknames) == 0 {
return ErrNoNicknames
}
var files []file
for _, nickname := range nicknames {
file, exists := r.files[nickname]
if !exists {
return ErrUnknownNickname
}
active := 0
for _, piece := range file.Pieces {
if piece.Active {
active++
}
}
if active < 3 {
return errors.New("Cannot share an inactive file")
}
files = append(files, *file)
}
// pipe data through json -> gzip -> w
zip, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
err := persist.Save(shareMetadata, files, zip)
if err != nil {
return err
}
zip.Close()
return nil
}
示例5: main
func main() {
flag.Parse()
if flag.NArg() != 1 || *helpFlag {
flag.PrintDefaults()
return
}
out, e := os.OpenFile(*outputFlag, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if e != nil {
log.Fatalf("Error while opening output file: %s", e.Error())
}
defer out.Close()
comp_out, e := gzip.NewWriterLevel(out, gzip.BestCompression)
if e != nil {
log.Fatalf("Error while setting up the compressor: %s", e.Error())
}
defer comp_out.Close()
cpio_out := cpio.NewWriter(comp_out)
defer cpio_out.Close()
in, e := os.Open(flag.Arg(0))
if e != nil {
log.Fatalf("Error while opening input file: %s", e.Error())
}
defer in.Close()
c := make(chan *Entry)
go parseInput(in, c)
createCpioArchive(cpio_out, c)
}
示例6: Encode
func (codec *GzipPayloadCodec) Encode(data []byte) []byte {
buf := bytes.NewBuffer([]byte{})
zipper, _ := gzip.NewWriterLevel(buf, gzip.BestSpeed)
zipper.Write(data)
zipper.Close()
return buf.Bytes()
}
示例7: NewProposal
// Constructor for a new Proposal given a Winlink Message.
//
// Reads the Winlink Message given and constructs a new proposal
// based on what's read and prepares for outbound delivery, returning
// a Proposal with the given data.
//
func NewProposal(MID, title string, code PropCode, data []byte) *Proposal {
prop := &Proposal{
mid: MID,
code: code,
msgType: "EM",
title: title,
size: len(data),
}
if prop.title == `` {
prop.title = `No title`
}
if prop.code == GzipProposal {
// Gzip compressed
var buf bytes.Buffer
z, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
if err != nil {
panic(err)
}
z.Write(data)
z.Close()
prop.compressedData = buf.Bytes()
} else {
// LZHUF compressed
prop.compressedData = lzhuf.Encode(data)
}
prop.compressedSize = len(prop.compressedData)
return prop
}
示例8: packFile
func packFile(path string) (*File, error) {
buf := new(bytes.Buffer)
zbuf, err := gzip.NewWriterLevel(buf, gzip.BestCompression)
if err != nil {
return nil, err
}
fr, err := os.Open(path)
if err != nil {
return nil, err
}
defer fr.Close()
fi, err := fr.Stat()
if err != nil {
return nil, err
}
_, err = io.Copy(zbuf, fr)
if err != nil {
return nil, err
}
zbuf.Close()
file := &File{
Path: filepath.Clean(path),
Mode: int64(fi.Mode()),
Data: buf.Bytes(),
}
return file, nil
}
示例9: writeToFile
func writeToFile(fpath string, ch chan []byte) error {
f, err := os.Create(fpath)
if err != nil {
return err
}
defer f.Close()
x.Check(err)
w := bufio.NewWriterSize(f, 1000000)
gw, err := gzip.NewWriterLevel(w, gzip.BestCompression)
if err != nil {
return err
}
for buf := range ch {
if _, err := gw.Write(buf); err != nil {
return err
}
}
if err := gw.Flush(); err != nil {
return err
}
if err := gw.Close(); err != nil {
return err
}
return w.Flush()
}
示例10: benchmarkOldGzipN
func benchmarkOldGzipN(b *testing.B, level int) {
dat, _ := ioutil.ReadFile("testdata/test.json")
dat = append(dat, dat...)
dat = append(dat, dat...)
dat = append(dat, dat...)
dat = append(dat, dat...)
dat = append(dat, dat...)
b.SetBytes(int64(len(dat)))
w, _ := oldgz.NewWriterLevel(ioutil.Discard, level)
b.ResetTimer()
for n := 0; n < b.N; n++ {
w.Reset(ioutil.Discard)
n, err := w.Write(dat)
if n != len(dat) {
panic("short write")
}
if err != nil {
panic(err)
}
err = w.Close()
if err != nil {
panic(err)
}
}
}
示例11: writeZone
func writeZone(z *Zone, w io.Writer) {
// this function panics on errors as all encoding errors are bugs and
// need to be manually resolved in the code.
z.lock()
defer z.unlock()
var data savedZone
data.X, data.Y, data.Z = z.X, z.Y, z.Z
data.Version = 0
data.TileData = make([]savedTile, 256*256)
i := 0
for x := 0; x < 256; x++ {
x8 := uint8(x)
for y := 0; y < 256; y++ {
y8 := uint8(y)
t := z.tile(x8, y8)
data.TileData[i].Version, data.TileData[i].Data = t.save()
i++
}
}
g, err := gzip.NewWriterLevel(w, gzip.BestCompression)
if err != nil {
panic(err)
}
defer g.Close()
err = gob.NewEncoder(g).Encode(&data)
if err != nil {
panic(err)
}
}
示例12: main
func main() {
flag.Parse()
c, _ := gzip.NewWriterLevel(os.Stdout, *n)
io.Copy(c, os.Stdin)
c.Close()
}
示例13: gzipCodeJS
// merge and gzip app javascript files into a single file, run once at startup
func gzipCodeJS() {
codeJSModTime = time.Now()
fileOut, _ := os.Create(codeJSFileName)
compressor, err := gzip.NewWriterLevel(fileOut, gzip.BestCompression)
if err != nil {
log.Fatal("gzipCodeJS", err)
}
files := []string{
"main.js",
"hub.js",
"data.js",
"lib.js",
"style.js",
"view_screen1.js",
"view_booktabs.js",
"view_noteedit.js",
"view_noteview.js",
"view_tabmgr.js",
"view_position.js",
}
var fileIn *os.File
for _, fileName := range files {
fileIn, err = os.Open("static/code/" + fileName)
if err != nil {
log.Fatalln("gzipCodeJS error: ", fileName, err)
}
io.Copy(compressor, fileIn)
fileIn.Close()
}
compressor.Close()
fileOut.Close()
}
示例14: PrepareArchive
func PrepareArchive(rootPath string) (string, int64, error) {
file, err := ioutil.TempFile("", "")
fail.Handle(err)
defer file.Close()
fileWriter := bufio.NewWriter(file)
defer fileWriter.Flush()
gzipWriter, err := gzip.NewWriterLevel(fileWriter, gzip.BestCompression)
fail.Handle(err)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
fullRootPath, err := filepath.Abs(rootPath)
fail.Handle(err)
err = addAllToArchive(fullRootPath, tarWriter)
if err != nil {
return "", 0, err
}
tarWriter.Close()
gzipWriter.Close()
fileWriter.Flush()
file.Close()
fileInfo, err := os.Stat(file.Name())
fail.Handle(err)
return file.Name(), fileInfo.Size(), nil
}
示例15: Header
// The header must be written before any content may be written.
func (w *Writer) Header(t Type, compression Comp, blockSize int) error {
if w.hasHeader {
return ErrHeaderOnce
}
w.t = t
w.hasHeader = true
var err error
err = writeHeader(w, blockSize, t, compression)
if err != nil {
return err
}
switch compression {
case CompNone:
w.body = w.Writer
case CompGZip:
w.body, err = gzip.NewWriterLevel(w.Writer, gzip.BestCompression)
if err != nil {
return err
}
default:
return ErrUnknownCompression
}
return nil
}