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


Golang flate.NewWriter函數代碼示例

本文整理匯總了Golang中compress/flate.NewWriter函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewWriter函數的具體用法?Golang NewWriter怎麽用?Golang NewWriter使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewWriter函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Deflate

// Deflate utility that compresses a string using the flate algo
func Deflate(inflated string) []byte {
	var b bytes.Buffer
	w, _ := flate.NewWriter(&b, -1)
	w.Write([]byte(inflated))
	w.Close()
	return b.Bytes()
}
開發者ID:wayf-dk,項目名稱:gosaml,代碼行數:8,代碼來源:gosaml.go

示例2: Body

func (output *BeegoOutput) Body(content []byte) {
	output_writer := output.res.(io.Writer)
	if output.EnableGzip == true && output.Context.Input.Header("Accept-Encoding") != "" {
		splitted := strings.SplitN(output.Context.Input.Header("Accept-Encoding"), ",", -1)
		encodings := make([]string, len(splitted))

		for i, val := range splitted {
			encodings[i] = strings.TrimSpace(val)
		}
		for _, val := range encodings {
			if val == "gzip" {
				output.Header("Content-Encoding", "gzip")
				output_writer, _ = gzip.NewWriterLevel(output.res, gzip.BestSpeed)

				break
			} else if val == "deflate" {
				output.Header("Content-Encoding", "deflate")
				output_writer, _ = flate.NewWriter(output.res, flate.BestSpeed)
				break
			}
		}
	} else {
		output.Header("Content-Length", strconv.Itoa(len(content)))
	}
	output_writer.Write(content)
	switch output_writer.(type) {
	case *gzip.Writer:
		output_writer.(*gzip.Writer).Close()
	case *flate.Writer:
		output_writer.(*flate.Writer).Close()
	}
}
開發者ID:Neeke,項目名稱:beego,代碼行數:32,代碼來源:output.go

示例3: 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

示例4: compressor

func compressor(fw *Writer, toCompress <-chan *writerBlock, toWrite chan<- *writerBlock) {
	switch fw.CompressionCodec {
	case CompressionDeflate:
		bb := new(bytes.Buffer)
		comp, _ := flate.NewWriter(bb, flate.DefaultCompression)
		for block := range toCompress {
			_, block.err = comp.Write(block.encoded.Bytes())
			block.err = comp.Close()
			if block.err == nil {
				block.compressed = bb.Bytes()
				toWrite <- block
			}
			bb = new(bytes.Buffer)
			comp.Reset(bb)
		}
	case CompressionNull:
		for block := range toCompress {
			block.compressed = block.encoded.Bytes()
			toWrite <- block
		}
	case CompressionSnappy:
		for block := range toCompress {
			block.compressed = snappy.Encode(block.compressed, block.encoded.Bytes())
			toWrite <- block
		}
	}
	close(toWrite)
}
開發者ID:hhkbp2,項目名稱:goavro,代碼行數:28,代碼來源:ocf_writer.go

示例5: Save

// Save persists a Siegfried struct to disk (path)
func (s *Siegfried) Save(path string) error {
	ls := persist.NewLoadSaver(nil)
	ls.SaveTime(s.C)
	s.em.Save(ls)
	s.cm.Save(ls)
	s.bm.Save(ls)
	s.tm.Save(ls)
	ls.SaveTinyUInt(len(s.ids))
	for _, i := range s.ids {
		i.Save(ls)
	}
	if ls.Err != nil {
		return ls.Err
	}
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = f.Write(append(config.Magic(), byte(config.Version()[0]), byte(config.Version()[1])))
	if err != nil {
		return err
	}
	z, err := flate.NewWriter(f, 1)
	if err != nil {
		return err
	}
	_, err = z.Write(ls.Bytes())
	z.Close()
	return err
}
開發者ID:TidyHuang,項目名稱:siegfried,代碼行數:32,代碼來源:siegfried.go

示例6: writeTo

func (ent *RawEntry) writeTo(out io.Writer) error {
	var buf bytes.Buffer
	z, err := flate.NewWriter(&buf, 8)
	if err != nil {
		return err
	}
	if err := binary.Write(z, binary.LittleEndian, uint32(len(ent.LeafInput))); err != nil {
		return err
	}
	if _, err := z.Write(ent.LeafInput); err != nil {
		return err
	}
	if err := binary.Write(z, binary.LittleEndian, uint32(len(ent.ExtraData))); err != nil {
		return err
	}
	if _, err := z.Write(ent.ExtraData); err != nil {
		return err
	}
	if err := z.Close(); err != nil {
		return err
	}

	bytes := buf.Bytes()
	if err := binary.Write(out, binary.LittleEndian, uint32(len(bytes))); err != nil {
		return err
	}
	if _, err := out.Write(bytes); err != nil {
		return err
	}

	return nil
}
開發者ID:mozkeeler,項目名稱:certificatetransparency,代碼行數:32,代碼來源:ct.go

示例7: CompressString

func CompressString(in string) string {
	buf := new(bytes.Buffer)
	compressor, _ := flate.NewWriter(buf, 9)
	compressor.Write([]byte(in))
	compressor.Close()
	return buf.String()
}
開發者ID:skimata,項目名稱:go-saml,代碼行數:7,代碼來源:compress.go

示例8: compressResponse

// conditionally compress the HTTP response. this function must be executed
// after all response headers have been set by the underlying handler (because
// it needs to inspect them) but before they have been written to the client
// (because it needs to change the headers and the data writer).
func compressResponse(w *ResponseWriter, req *http.Request) {
	// rudimentary "can this be compressed" check
	if !compressable(w.Header().Get("Content-Type")) {
		return
	}
	// do not re-encode
	if w.Header().Get("Content-Encoding") != "" {
		return
	}
	ae := req.Header.Get("Accept-Encoding")
	// no q for u
	switch {
	case strings.Contains(ae, "gzip"):
		w.WrapBodyWriter(func(w io.Writer) io.Writer {
			return gzip.NewWriter(w)
		})
		w.Header().Set("Content-Encoding", "gzip")
		w.Header().Del("content-length")
		break
	case strings.Contains(ae, "deflate"):
		w.WrapBodyWriter(func(w io.Writer) io.Writer {
			def, _ := flate.NewWriter(w, flate.DefaultCompression)
			return def
		})
		w.Header().Set("Content-Encoding", "deflate")
		w.Header().Del("content-length")
		break
	}
}
開發者ID:xyproto,項目名稱:web,代碼行數:33,代碼來源:compress.go

示例9: Compress

func (data *Data) Compress() error {
	if data.IsCompressed() || len(data.Content) < 32 {
		// already compressed, or not worth compressing
		// because data is nil or too short
		return nil
	}

	buf := bytes.Buffer{}
	w, err := flate.NewWriter(&buf, flate.BestCompression)
	if err != nil {
		return err
	}
	n, err := w.Write(data.Content)
	if err != nil {
		return err
	}
	if n != len(data.Content) {
		return errs.ServerError("cannot compress")
	}
	err = w.Close()
	if err != nil {
		return err
	}
	compressedBytes := buf.Bytes()

	if len(compressedBytes) < len(data.Content) {
		data.UncompressedLength = len(data.Content)
		data.Content = compressedBytes
		data.ContentEncoding = ceDeflate
	}

	return nil
}
開發者ID:software-projects,項目名稱:go-exp,代碼行數:33,代碼來源:data.go

示例10: NewLogger

//NewLogger creates a new Logger instance
func NewLogger(directory string) (*Logger, error) {
	var err error
	if directory == "" {
		if directory, err = ioutil.TempDir("", "dlog"); err != nil {
			return nil, err
		}
	}
	os.MkdirAll(directory, 0755)
	filePath := filepath.Join(directory, "dlog.bin")

	wFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		log.Fatal(err)
	}
	w, _ := flate.NewWriter(wFile, flate.BestCompression)

	l := &Logger{
		wChan: make(chan model.LogEntry, 1000),
		wFile: wFile,
	}

	go l.writeRoutine(w)

	return l, nil
}
開發者ID:netbrain,項目名稱:goatpad,代碼行數:26,代碼來源:dlog.go

示例11: handlerError

func handlerError(rw http.ResponseWriter, html string, code int) {
	var b bytes.Buffer
	w, err := flate.NewWriter(&b, flate.BestCompression)
	if err != nil {
		rw.Header().Set("Content-Type", "text/plain")
		rw.WriteHeader(http.StatusBadGateway)
		io.WriteString(rw, err.Error())
	}

	fmt.Fprintf(w, "HTTP/1.1 %d\r\n", code)
	fmt.Fprintf(w, "Content-Type: text/html; charset=utf-8\r\n")
	fmt.Fprintf(w, "Content-Length: %d\r\n", len(html))
	io.WriteString(w, "\r\n")
	io.WriteString(w, html)
	w.Close()

	b0 := make([]byte, 2)
	binary.BigEndian.PutUint16(b0, uint16(b.Len()))

	rw.Header().Set("Content-Type", "image/gif")
	rw.Header().Set("Content-Length", strconv.Itoa(len(b0)+b.Len()))
	rw.WriteHeader(http.StatusOK)
	rw.Write(b0)
	rw.Write(b.Bytes())
}
開發者ID:sunjiahz,項目名稱:goproxy,代碼行數:25,代碼來源:gae.go

示例12: BenchmarkWriteWithBestDeflateCompression

func BenchmarkWriteWithBestDeflateCompression(b *testing.B) {
	file := benchFile()
	w, _ := flate.NewWriter(file, flate.BestSpeed)
	benchmarkWrite(b, w)
	fi, _ := file.Stat()
	b.SetBytes(int64(int(fi.Size()) / 1024 / 1024))
}
開發者ID:netbrain,項目名稱:goatpad,代碼行數:7,代碼來源:dlog_test.go

示例13: 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

示例14: Flate

func Flate(r io.Writer) (c Compressor) {
	var err error
	if c, err = flate.NewWriter(r, -1); err != nil {
		panic(err)
	}
	return
}
開發者ID:TShadwell,項目名稱:fweight,代碼行數:7,代碼來源:compression.go

示例15: deflate

func deflate(inp []byte) ([]byte, error) {
	out := new(bytes.Buffer)
	writer, _ := flate.NewWriter(out, 1)
	io.Copy(writer, bytes.NewBuffer(inp))
	err := writer.Close()
	return out.Bytes(), err
}
開發者ID:blinkat,項目名稱:go-invoker,代碼行數:7,代碼來源:encoding.go


注:本文中的compress/flate.NewWriter函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。