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


Golang gzip.NewWriter函數代碼示例

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


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

示例1: setBodyGzip

// setBodyGzip gzip's the body. It accepts both strings and structs as body.
// The latter will be encoded via json.Marshal.
func (r *Request) setBodyGzip(body interface{}) error {
	switch b := body.(type) {
	case string:
		buf := new(bytes.Buffer)
		w := gzip.NewWriter(buf)
		if _, err := w.Write([]byte(b)); err != nil {
			return err
		}
		if err := w.Close(); err != nil {
			return err
		}
		r.Header.Add("Content-Encoding", "gzip")
		r.Header.Add("Vary", "Accept-Encoding")
		return r.setBodyReader(bytes.NewReader(buf.Bytes()))
	default:
		data, err := json.Marshal(b)
		if err != nil {
			return err
		}
		buf := new(bytes.Buffer)
		w := gzip.NewWriter(buf)
		if _, err := w.Write(data); err != nil {
			return err
		}
		if err := w.Close(); err != nil {
			return err
		}
		r.Header.Add("Content-Encoding", "gzip")
		r.Header.Add("Vary", "Accept-Encoding")
		r.Header.Set("Content-Type", "application/json")
		return r.setBodyReader(bytes.NewReader(buf.Bytes()))
	}
}
開發者ID:caesarxuchao,項目名稱:heapster,代碼行數:35,代碼來源:request.go

示例2: saveSplitPages

func saveSplitPages(input <-chan *PageContainer) {
	featuredFile, err := os.Create(featuredFilePath)
	if err != nil {
		panic(err)
	}
	featuredCompressed := gzip.NewWriter(featuredFile)
	defer featuredFile.Close()

	normalFile, err := os.Create(normalFilePath)
	if err != nil {
		panic(err)
	}
	normalCompressed := gzip.NewWriter(normalFile)
	defer normalFile.Close()

	featuredChannel, featuredWriter := ArticleWriter(featuredCompressed)
	normalChannel, normalWriter := ArticleWriter(normalCompressed)
	//featuredChannel, featuredWriter := ArticleWriter(featuredFile)
	//normalChannel, normalWriter := ArticleWriter(normalFile)

	DistrbuteArticles(input, featuredChannel, normalChannel)

	// Wait for all writers to finish
	<-featuredWriter
	<-normalWriter

	// Close all the gzip streams
	// I tried defering the close
	// call but kept getting some EOF errors
	featuredCompressed.Close()
	normalCompressed.Close()
}
開發者ID:jcla1,項目名稱:wikipedia_analyser,代碼行數:32,代碼來源:data_savers.go

示例3: init

func init() {

	RootDirectory.Set("/vfiles", func() *VDir {
		var dir = NewVDir("/vfiles", "../vfiles", "/home/alex/local/cmd/src/github.com/influx6/assets/vfiles", false)

		// register the sub-directories

		// register the files

		dir.AddFile(NewVFile("/home/alex/local/cmd/src/github.com/influx6/assets/tests", "/vfiles/vfiles.go", "../vfiles/vfiles.go", 18244, true, false, func(v *VFile) ([]byte, error) {
			fo, err := os.Open(v.RealPath())
			if err != nil {
				return nil, fmt.Errorf("---> assets.readFile: Error reading file: %s at %s: %s\n", v.Name(), v.RealPath(), err)
			}

			defer fo.Close()

			var buf bytes.Buffer
			gz := gzip.NewWriter(&buf)

			_, err = io.Copy(gz, fo)
			gz.Close()

			if err != nil {
				return nil, fmt.Errorf("---> assets.readFile.gzip: Error gzipping file: %s at %s: %s\n", v.Name(), v.RealPath(), err)
			}

			return buf.Bytes(), nil
		}))

		dir.AddFile(NewVFile("/home/alex/local/cmd/src/github.com/influx6/assets/tests", "/vfiles/vfiles_test.go", "../vfiles/vfiles_test.go", 4132, true, false, func(v *VFile) ([]byte, error) {
			fo, err := os.Open(v.RealPath())
			if err != nil {
				return nil, fmt.Errorf("---> assets.readFile: Error reading file: %s at %s: %s\n", v.Name(), v.RealPath(), err)
			}

			defer fo.Close()

			var buf bytes.Buffer
			gz := gzip.NewWriter(&buf)

			_, err = io.Copy(gz, fo)
			gz.Close()

			if err != nil {
				return nil, fmt.Errorf("---> assets.readFile.gzip: Error gzipping file: %s at %s: %s\n", v.Name(), v.RealPath(), err)
			}

			return buf.Bytes(), nil
		}))

		return dir
	}())

}
開發者ID:influx6,項目名稱:assets,代碼行數:55,代碼來源:debug.go

示例4: TestAssetsDir

func TestAssetsDir(t *testing.T) {
	// For any given request to $FILE, we should return the first found of
	//  - assetsdir/$THEME/$FILE
	//  - compiled in asset $THEME/$FILE
	//  - assetsdir/default/$FILE
	//  - compiled in asset default/$FILE

	// The asset map contains compressed assets, so create a couple of gzip compressed assets here.
	buf := new(bytes.Buffer)
	gw := gzip.NewWriter(buf)
	gw.Write([]byte("default"))
	gw.Close()
	def := buf.Bytes()

	buf = new(bytes.Buffer)
	gw = gzip.NewWriter(buf)
	gw.Write([]byte("foo"))
	gw.Close()
	foo := buf.Bytes()

	e := &staticsServer{
		theme:    "foo",
		mut:      sync.NewRWMutex(),
		assetDir: "testdata",
		assets: map[string][]byte{
			"foo/a":     foo, // overridden in foo/a
			"foo/b":     foo,
			"default/a": def, // overridden in default/a (but foo/a takes precedence)
			"default/b": def, // overridden in default/b (but foo/b takes precedence)
			"default/c": def,
		},
	}

	s := httptest.NewServer(e)
	defer s.Close()

	// assetsdir/foo/a exists, overrides compiled in
	expectURLToContain(t, s.URL+"/a", "overridden-foo")

	// foo/b is compiled in, default/b is overridden, return compiled in
	expectURLToContain(t, s.URL+"/b", "foo")

	// only exists as compiled in default/c so use that
	expectURLToContain(t, s.URL+"/c", "default")

	// only exists as overridden default/d so use that
	expectURLToContain(t, s.URL+"/d", "overridden-default")
}
開發者ID:letiemble,項目名稱:syncthing,代碼行數:48,代碼來源:gui_test.go

示例5: Open

// Open is part of the intents.file interface. realBSONFiles need to have Open called before
// Read can be called
func (f *realBSONFile) Open() (err error) {
	if f.path == "" {
		// This should not occur normally. All realBSONFile's should have a path
		return fmt.Errorf("error creating BSON file without a path, namespace: %v",
			f.intent.Namespace())
	}
	err = os.MkdirAll(filepath.Dir(f.path), os.ModeDir|os.ModePerm)
	if err != nil {
		return fmt.Errorf("error creating directory for BSON file %v: %v",
			filepath.Dir(f.path), err)
	}

	fileName := f.path
	file, err := os.Create(fileName)
	if err != nil {
		return fmt.Errorf("error creating BSON file %v: %v", fileName, err)
	}
	var writeCloser io.WriteCloser
	if f.gzip {
		writeCloser = gzip.NewWriter(file)
	} else {
		// wrap writer in buffer to reduce load on disk
		writeCloser = writeFlushCloser{
			atomicFlusher{
				bufio.NewWriterSize(file, 32*1024),
			},
		}
	}
	f.WriteCloser = &wrappedWriteCloser{
		WriteCloser: writeCloser,
		inner:       file,
	}

	return nil
}
開發者ID:gabrielrussell,項目名稱:mongo-tools,代碼行數:37,代碼來源:prepare.go

示例6: TarFilelist

// file name filelist is like this: './source/file'
func TarFilelist(filelist []string, case_dir string, object_name string) (tar_url string) {
	tar_url = path.Join(case_dir, object_name) + ".tar.gz"
	fw, err := os.Create(tar_url)
	if err != nil {
		fmt.Println("Failed in create tar file ", err)
		return tar_url
	}
	defer fw.Close()
	gw := gzip.NewWriter(fw)
	defer gw.Close()
	tw := tar.NewWriter(gw)
	defer tw.Close()

	for index := 0; index < len(filelist); index++ {
		source_file := filelist[index]
		fi, err := os.Stat(path.Join(case_dir, source_file))
		if err != nil {
			fmt.Println(err)
			continue
		}
		fr, err := os.Open(path.Join(case_dir, source_file))
		if err != nil {
			fmt.Println(err)
			continue
		}
		h := new(tar.Header)
		h.Name = source_file
		h.Size = fi.Size()
		h.Mode = int64(fi.Mode())
		h.ModTime = fi.ModTime()
		err = tw.WriteHeader(h)
		_, err = io.Copy(tw, fr)
	}
	return tar_url
}
開發者ID:hqhq,項目名稱:oct,代碼行數:36,代碼來源:libocit.go

示例7: RunTestGzip

func RunTestGzip(data []byte) {
	log.Printf("encoding/RunTestGzip: Testing comprssion Gzip\n")

	var compressed bytes.Buffer
	w := gzip.NewWriter(&compressed)
	defer w.Close()
	now := time.Now()
	w.Write(data)

	cl := compressed.Len()
	log.Printf("encoding/RunTestGzip: Compressed from %d bytes to %d bytes in %d ns\n", len(data), cl, time.Since(now).Nanoseconds())

	recovered := make([]byte, len(data))
	r, _ := gzip.NewReader(&compressed)
	defer r.Close()

	total := 0
	n := 100
	var err error = nil
	for err != io.EOF && n != 0 {
		n, err = r.Read(recovered[total:])
		total += n
	}
	log.Printf("encoding/RunTestGzip: Uncompressed from %d bytes to %d bytes in %d ns\n", cl, len(recovered), time.Since(now).Nanoseconds())
}
開發者ID:jmptrader,項目名稱:encoding,代碼行數:25,代碼來源:benchtools.go

示例8: NewCompressedResponseWriter

func NewCompressedResponseWriter(responseWriter http.ResponseWriter) *CompressedResponseWriter {
	crw := &CompressedResponseWriter{
		responseWriter: responseWriter,
		gz:             gzip.NewWriter(responseWriter),
	}
	return crw
}
開發者ID:ubaltzer,項目名稱:gofoomo,代碼行數:7,代碼來源:compressed_response_writer.go

示例9: checksumForGraphIDNoTarsplit

func (c *checksums) checksumForGraphIDNoTarsplit(id, parent, newTarDataPath string) (diffID layer.DiffID, size int64, err error) {
	rawarchive, err := c.driver.TarStream(id, parent)
	if err != nil {
		return
	}
	defer rawarchive.Close()

	f, err := os.Create(newTarDataPath)
	if err != nil {
		return
	}
	defer f.Close()
	mfz := gzip.NewWriter(f)
	defer mfz.Close()
	metaPacker := storage.NewJSONPacker(mfz)

	packerCounter := &packSizeCounter{metaPacker, &size}

	archive, err := asm.NewInputTarStream(rawarchive, packerCounter, nil)
	if err != nil {
		return
	}
	dgst, err := digest.FromReader(archive)
	if err != nil {
		return
	}
	diffID = layer.DiffID(dgst)
	return
}
開發者ID:docker,項目名稱:v1.10-migrator,代碼行數:29,代碼來源:checksums.go

示例10: gzipStr

func gzipStr(s string) []byte {
	var b bytes.Buffer
	w := gzip.NewWriter(&b)
	io.WriteString(w, s)
	w.Close()
	return b.Bytes()
}
開發者ID:bsm,項目名稱:gziphandler,代碼行數:7,代碼來源:gzip_test.go

示例11: openLogFile

func openLogFile(fileName, fileType string, compress bool, rotateMode int, maxSize int64) (*jsonFile, error) {
	fullName := fileName + fileType
	if _, err := os.Stat(fullName); err == nil {
		os.Rename(fullName, fileName+".01"+fileType)
		fullName = fileName + ".02" + fileType
	} else if _, err := os.Stat(fileName + ".01" + fileType); err == nil {
		for fileId := 1; true; fileId++ {
			fullName = fileName + fmt.Sprintf(".%02d", fileId) + fileType
			if _, err := os.Stat(fullName); err != nil {
				break
			}
		}
	}
	file, err := os.OpenFile(fullName, os.O_WRONLY|os.O_CREATE, 0755)
	if err != nil {
		return nil, err
	}
	jsonfile := &jsonFile{file: file, curFile: fullName, rotateMode: rotateMode}
	if compress {
		jsonfile.bufio = bufio.NewWriter(jsonfile.file)
		jsonfile.gzip = gzip.NewWriter(jsonfile.bufio)
		jsonfile.json = json.NewEncoder(jsonfile.gzip)
	} else {
		jsonfile.bufio = bufio.NewWriter(jsonfile.file)
		jsonfile.json = json.NewEncoder(jsonfile.bufio)
	}
	if jsonfile.rotateMode == ROTATE_BY_SIZE && maxSize == 0 {
		jsonfile.maxSize = 1024 * 1024 * 1024 * 25
	} else {
		jsonfile.maxSize = maxSize
	}

	return jsonfile, nil
}
開發者ID:aisondhs,項目名稱:alog,代碼行數:34,代碼來源:jsonlog.go

示例12: TarGz

// TarGz implementation of Archiver.
func TarGz(archiveFilename string, itemsToArchive []ArchiveItem) error {
	// file write
	fw, err := os.Create(archiveFilename)
	if err != nil {
		return err
	}
	defer fw.Close()

	// gzip write
	gw := gzip.NewWriter(fw)
	defer gw.Close()

	// tar write
	tw := tar.NewWriter(gw)
	defer tw.Close()

	for _, item := range itemsToArchive {
		err = addItemToTarGz(item, tw)
		if err != nil {
			return err
		}
	}
	err = tw.Close()
	return err
}
開發者ID:himanshugpt,項目名稱:evergreen,代碼行數:26,代碼來源:targz.go

示例13: walkerFor

func walkerFor(basePath string) filepath.WalkFunc {
	return func(name string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if strings.HasPrefix(filepath.Base(name), ".") {
			// Skip dotfiles
			return nil
		}

		if info.Mode().IsRegular() {
			fd, err := os.Open(name)
			if err != nil {
				return err
			}

			var buf bytes.Buffer
			gw := gzip.NewWriter(&buf)
			io.Copy(gw, fd)
			fd.Close()
			gw.Flush()
			gw.Close()

			name, _ = filepath.Rel(basePath, name)
			assets = append(assets, asset{
				Name: filepath.ToSlash(name),
				Data: base64.StdEncoding.EncodeToString(buf.Bytes()),
			})
		}

		return nil
	}
}
開發者ID:qbit,項目名稱:syncthing,代碼行數:34,代碼來源:main.go

示例14: Add

func (c *dynamoDBCollector) Add(ctx context.Context, rep report.Report) error {
	userid, err := c.userIDer(ctx)
	if err != nil {
		return err
	}

	var buf bytes.Buffer
	writer := gzip.NewWriter(&buf)
	if err := codec.NewEncoder(writer, &codec.MsgpackHandle{}).Encode(&rep); err != nil {
		return err
	}
	writer.Close()

	now := time.Now()
	rowKey := fmt.Sprintf("%s-%s", userid, strconv.FormatInt(now.UnixNano()/time.Hour.Nanoseconds(), 10))
	_, err = c.db.PutItem(&dynamodb.PutItemInput{
		TableName: aws.String(tableName),
		Item: map[string]*dynamodb.AttributeValue{
			hourField: {
				S: aws.String(rowKey),
			},
			tsField: {
				N: aws.String(strconv.FormatInt(now.UnixNano(), 10)),
			},
			reportField: {
				B: buf.Bytes(),
			},
		},
	})
	if err != nil {
		return err
	}
	return nil
}
開發者ID:dilgerma,項目名稱:scope,代碼行數:34,代碼來源:dynamo_collector.go

示例15: HandleGetHasRefs

func HandleGetHasRefs(w http.ResponseWriter, req *http.Request, ps URLParams, ds DataStore) {
	err := d.Try(func() {
		d.Exp.Equal("POST", req.Method)

		req.ParseForm()
		refStrs := req.PostForm["ref"]
		d.Exp.True(len(refStrs) > 0)

		refs := make([]ref.Ref, len(refStrs))
		for idx, refStr := range refStrs {
			refs[idx] = ref.Parse(refStr)
		}

		w.Header().Add("Content-Type", "text/plain")
		writer := w.(io.Writer)
		if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
			w.Header().Add("Content-Encoding", "gzip")
			gw := gzip.NewWriter(w)
			defer gw.Close()
			writer = gw
		}

		sz := chunks.NewSerializer(writer)
		for _, r := range refs {
			has := ds.transitionalChunkStore().Has(r)
			fmt.Fprintf(writer, "%s %t\n", r, has)
		}
		sz.Close()
	})

	if err != nil {
		http.Error(w, fmt.Sprintf("Error: %v", err), http.StatusBadRequest)
		return
	}
}
開發者ID:arv,項目名稱:noms-old,代碼行數:35,代碼來源:handlers.go


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