• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang gzip.NewWriterLevel函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中compress/gzip.NewWriterLevel函数的典型用法代码示例。如果您正苦于以下问题:Golang NewWriterLevel函数的具体用法?Golang NewWriterLevel怎么用?Golang NewWriterLevel使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewWriterLevel函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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}
}
开发者ID:zaolab,项目名称:sunnified,代码行数:30,代码来源:gzip.go


示例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
}
开发者ID:tokopedia,项目名称:routes,代码行数:30,代码来源:routes.go


示例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
}
开发者ID:EMSSConsulting,项目名称:Thargo,代码行数:30,代码来源:archive.go


示例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
}
开发者ID:mm3,项目名称:Sia,代码行数:36,代码来源:persist.go


示例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)
}
开发者ID:akrennmair,项目名称:gobox,代码行数:30,代码来源:geninitramfs.go


示例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()
}
开发者ID:rsrsps,项目名称:kafka,代码行数:7,代码来源:payload_codec.go


示例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
}
开发者ID:LA3QMA,项目名称:wl2k-go,代码行数:38,代码来源:proposal.go


示例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
}
开发者ID:kisom,项目名称:cryptutils,代码行数:32,代码来源:passcrypt.go


示例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()
}
开发者ID:dgraph-io,项目名称:dgraph,代码行数:27,代码来源:backup.go


示例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)
		}
	}
}
开发者ID:zhuweijava,项目名称:compress,代码行数:26,代码来源:gzip_test.go


示例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)
	}
}
开发者ID:BenLubar,项目名称:Rnoadm,代码行数:33,代码来源:storage.go


示例12: main

func main() {
	flag.Parse()
	c, _ := gzip.NewWriterLevel(os.Stdout, *n)

	io.Copy(c, os.Stdin)
	c.Close()
}
开发者ID:ibmendoza,项目名称:golang-examples,代码行数:7,代码来源:compress.go


示例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()
}
开发者ID:txjmp,项目名称:noteburt,代码行数:34,代码来源:web.go


示例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
}
开发者ID:rootwzrd,项目名称:appstax-cli,代码行数:28,代码来源:hosting.go


示例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
}
开发者ID:xiocode,项目名称:rsync,代码行数:26,代码来源:proto.go


示例16: Gzip

func Gzip(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		// @TODO(mark): Swap this for some handler flags
		url, err := url.Parse(r.URL.String())
		queryParams := url.Query()
		_, found := queryParams[queryStringKey]
		if found {
			handler.ServeHTTP(w, r)
			return
		}

		if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
			handler.ServeHTTP(w, r)
			return
		}

		w.Header().Set("Content-Encoding", "gzip")
		gz, err := gzip.NewWriterLevel(w, gzip.BestCompression)
		if err != nil {
			http.Error(w, "Error with gzip compression", http.StatusInternalServerError)
			return
		}
		defer gz.Close()

		gzw := gzipResponseWriter{
			Writer:         gz,
			ResponseWriter: w,
		}
		handler.ServeHTTP(gzw, r)
	})
}
开发者ID:Ganners,项目名称:Ganners-Webserver,代码行数:32,代码来源:gzip_handler.go


示例17: writeItem

// Writes the given file into tarfn
func writeItem(tarfn string, fn string) (pos uint64, err error) {
	pos = 0
	sfh, err := os.Open(fn)
	defer sfh.Close()
	if sfi, err := sfh.Stat(); err == nil {
		hdr := new(tar.Header)
		hdr.Name = sfi.Name()
		hdr.Size = sfi.Size()
		hdr.Mode = int64(sfi.Mode().Perm())
		hdr.ModTime = sfi.ModTime()
		var (
			tw *tar.Writer
			f  ReadWriteSeekCloser
		)
		if tw, f, pos, err = OpenForAppend(tarfn); err == nil {
			defer f.Close()
			defer tw.Close() //LIFO
			if err := tw.WriteHeader(hdr); err == nil {
				if zw, err := gzip.NewWriterLevel(tw, flate.BestCompression); err == nil {
					defer zw.Close()
					_, err = io.Copy(zw, sfh)
				}
			}
		}
	}
	return
}
开发者ID:tgulacsi,项目名称:aostor,代码行数:28,代码来源:tarhelper.go


示例18: Gzip

func Gzip(level int) gin.HandlerFunc {
	var gzPool sync.Pool
	gzPool.New = func() interface{} {
		gz, err := gzip.NewWriterLevel(ioutil.Discard, level)
		if err != nil {
			panic(err)
		}
		return gz
	}
	return func(c *gin.Context) {
		if !shouldCompress(c.Request) {
			return
		}

		gz := gzPool.Get().(*gzip.Writer)
		defer gzPool.Put(gz)
		gz.Reset(c.Writer)

		c.Header("Content-Encoding", "gzip")
		c.Header("Vary", "Accept-Encoding")
		c.Writer = &gzipWriter{c.Writer, gz}
		defer func() {
			c.Header("Content-Length", "0")
			gz.Close()
		}()
		c.Next()
	}
}
开发者ID:gin-contrib,项目名称:gzip,代码行数:28,代码来源:gzip.go


示例19: save

// save saves a file to w in shareable form. Files are stored in binary format
// and gzipped to reduce size.
func (f *file) save(w io.Writer) error {
	// TODO: error checking
	zip, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
	defer zip.Close()
	enc := encoding.NewEncoder(zip)

	// encode easy fields
	enc.Encode(f.name)
	enc.Encode(f.size)
	enc.Encode(f.masterKey)
	enc.Encode(f.pieceSize)
	enc.Encode(f.mode)
	enc.Encode(f.bytesUploaded)
	enc.Encode(f.chunksUploaded)

	// encode erasureCode
	switch code := f.erasureCode.(type) {
	case *rsCode:
		enc.Encode("Reed-Solomon")
		enc.Encode(uint64(code.dataPieces))
		enc.Encode(uint64(code.numPieces - code.dataPieces))
	default:
		if build.DEBUG {
			panic("unknown erasure code")
		}
		return errors.New("unknown erasure code")
	}
	// encode contracts
	enc.Encode(uint64(len(f.contracts)))
	for _, c := range f.contracts {
		enc.Encode(c)
	}
	return nil
}
开发者ID:dlmac,项目名称:Sia,代码行数:36,代码来源:persist.go


示例20: getZip

// getZip is equivalent to `ipfs getdag $hash | gzip`
func getZip(ctx context.Context, node *core.IpfsNode, p path.Path, compression int) (io.Reader, error) {
	dagnode, err := core.Resolve(ctx, node, p)
	if err != nil {
		return nil, err
	}

	reader, err := uio.NewDagReader(ctx, dagnode, node.DAG)
	if err != nil {
		return nil, err
	}

	pr, pw := io.Pipe()
	gw, err := gzip.NewWriterLevel(pw, compression)
	if err != nil {
		return nil, err
	}
	bufin := bufio.NewReader(reader)
	go func() {
		_, err := bufin.WriteTo(gw)
		if err != nil {
			log.Error("Fail to compress the stream")
		}
		gw.Close()
		pw.Close()
	}()

	return pr, nil
}
开发者ID:shuanzi,项目名称:go-ipfs,代码行数:29,代码来源:get.go



注:本文中的compress/gzip.NewWriterLevel函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang gzip.Reader类代码示例发布时间:2022-05-24
下一篇:
Golang gzip.NewWriter函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap