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

Golang flate.Writer类代码示例

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

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



在下文中一共展示了Writer类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: ExampleWriter_RegisterCompressor

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.
}
开发者ID:rogpeppe,项目名称:zip,代码行数:27,代码来源:example_test.go


示例2: Zip

func Zip(unBytes []byte, contentEncoding string) (zipBytes []byte, err error) {

	switch contentEncoding {
	case "":
		zipBytes = unBytes
		break

	case "gzip":
		var bf bytes.Buffer
		gw := gzip.NewWriter(&bf)
		gw.Write(unBytes)
		gw.Close()

		zipBytes = bf.Bytes()

	case "deflate":
		var bf bytes.Buffer
		var gw *flate.Writer
		gw, err = flate.NewWriter(&bf, -1)
		if err != nil {
			return
		}
		gw.Write(unBytes)
		gw.Close()

		zipBytes = bf.Bytes()

	default:
		err = fmt.Errorf("Zip Unkown Content-Encoding: %v", contentEncoding)
	}

	return
}
开发者ID:uileyar,项目名称:helloworld,代码行数:33,代码来源:network.go


示例3: main

func main() {
	var file *os.File
	var err error
	var writer *flate.Writer
	var body []byte

	if file, err = os.Create("output/sample.tar.flate"); err != nil {
		log.Fatalln(err)
	}
	defer file.Close()

	// flateはレベルを指定する。flate.BestSpeed or flate.BestCompression or DefaultCompression
	if writer, err = flate.NewWriter(file, flate.BestCompression); err != nil {
		log.Fatalln(err)
	}
	defer writer.Close()

	var filepaths = []string{
		"files/b0044482_1413812.jpg",
		"files/dart_flight_school.png",
		"files/golang.txt",
	}

	//	buf := new(bytes.Buffer)
	//	tw := tar.NewWriter(buf)
	tw := tar.NewWriter(writer)
	defer tw.Close()

	for _, filepath := range filepaths {
		if body, err = ioutil.ReadFile(filepath); err != nil {
			log.Fatalln(err)
		}

		if body != nil {
			hdr := &tar.Header{
				Name: path.Base(filepath),
				Size: int64(len(body)),
			}
			if err := tw.WriteHeader(hdr); err != nil {
				println(err)
			}
			if _, err := tw.Write(body); err != nil {
				println(err)
			}
		}
	}

	//	writer.Write(buf.Bytes())
	//	writer.Flush()
}
开发者ID:qt-luigi,项目名称:golangcafe,代码行数:50,代码来源:flate.go


示例4: Compress

func (self *FlateCompressor) Compress(src []byte) ([]byte, error) {
	var err error
	var compressor *flate.Writer
	cdest := bytes.NewBuffer(make([]byte, 0, len(src)))
	if self.dict == nil {
		compressor, err = flate.NewWriter(cdest, self.level)
	} else {
		compressor, err = flate.NewWriterDict(cdest, self.level, self.dict)
	}
	//compressor.Reset(cdest)
	compressor.Write(src)
	err = compressor.Close()
	if err != nil {
		fmt.Println("Compress Close err:%s", err.Error())
	}
	return cdest.Bytes(), err
}
开发者ID:navy1125,项目名称:gostudy,代码行数:17,代码来源:compresstest.go


示例5: deflate

func (e *Engine) deflate() error {
	var buf bytes.Buffer
	var level int
	var err error
	level, err = e.stack.PopInt()
	if err == nil {
		var w *flate.Writer
		w, err = flate.NewWriter(&buf, level)
		if err == nil {
			_, err = w.Write(e.stack.Pop())
			w.Close()
		}
	}
	if err == nil {
		e.stack.Push(buf.Bytes())
	}
	return err
}
开发者ID:ancientlore,项目名称:hashsrv,代码行数:18,代码来源:compress.go


示例6: readFile

func readFile(filePath string) (string, error) {
	var buf []byte
	var err error
	var n int
	var writer *flate.Writer
	var encoder io.WriteCloser
	var file *os.File
	var data *bytes.Buffer = new(bytes.Buffer)

	if file, err = os.Open(filePath); err != nil {
		return "", err
	}
	defer file.Close()

	encoder = ascii85.NewEncoder(data)
	defer encoder.Close()

	writer, err = flate.NewWriter(encoder, flate.BestCompression)
	defer writer.Close()

	buf = make([]byte, 8192)
	err = nil
	for {
		if n, err = file.Read(buf); err != nil {
			if err == io.EOF {
				break
			} else {
				return "", err
			}
		}

		if n == 8192 {
			_, err = writer.Write(buf)
		} else {
			_, err = writer.Write(buf[:n])
		}
		if err != nil {
			return "", err
		}
	}
	if err = writer.Flush(); err != nil {
		return "", err
	}

	return strconv.Quote(string(data.Bytes())), nil
}
开发者ID:phoenixxie,项目名称:resource,代码行数:46,代码来源:file.go


示例7: ReturnWriter

// ReturnWriter returns a flate.Writer to the pool that can
// late be reused via GetWriter.
// Don't close the writer, Flush will be called before returning
// it to the pool.
func (pool *DeflatePool) ReturnWriter(writer *flate.Writer) {
	writer.Close()
	pool.pool.Put(writer)
}
开发者ID:vincecima,项目名称:just-install,代码行数:8,代码来源:compression.go


示例8: releaseFlateWriter

func releaseFlateWriter(r *flate.Writer) {
	r.Reset(emptyBuffer) // release the previous io.Writer
	flateWriterPool.Put(r)
}
开发者ID:lestrrat,项目名称:go-saml,代码行数:4,代码来源:encode.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang gzip.NewReader函数代码示例发布时间:2022-05-24
下一篇:
Golang flate.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