當前位置: 首頁>>代碼示例>>Golang>>正文


Golang flate.Writer類代碼示例

本文整理匯總了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;未經允許,請勿轉載。