本文整理汇总了Golang中compress/gzip.Writer类的典型用法代码示例。如果您正苦于以下问题:Golang Writer类的具体用法?Golang Writer怎么用?Golang Writer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Writer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: tryPostMessages
func (l *splunkLogger) tryPostMessages(messages []*splunkMessage) error {
if len(messages) == 0 {
return nil
}
var buffer bytes.Buffer
var writer io.Writer
var gzipWriter *gzip.Writer
var err error
// If gzip compression is enabled - create gzip writer with specified compression
// level. If gzip compression is disabled, use standard buffer as a writer
if l.gzipCompression {
gzipWriter, err = gzip.NewWriterLevel(&buffer, l.gzipCompressionLevel)
if err != nil {
return err
}
writer = gzipWriter
} else {
writer = &buffer
}
for _, message := range messages {
jsonEvent, err := json.Marshal(message)
if err != nil {
return err
}
if _, err := writer.Write(jsonEvent); err != nil {
return err
}
}
// If gzip compression is enabled, tell it, that we are done
if l.gzipCompression {
err = gzipWriter.Close()
if err != nil {
return err
}
}
req, err := http.NewRequest("POST", l.url, bytes.NewBuffer(buffer.Bytes()))
if err != nil {
return err
}
req.Header.Set("Authorization", l.auth)
// Tell if we are sending gzip compressed body
if l.gzipCompression {
req.Header.Set("Content-Encoding", "gzip")
}
res, err := l.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
var body []byte
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return err
}
return fmt.Errorf("%s: failed to send event - %s - %s", driverName, res.Status, body)
}
io.Copy(ioutil.Discard, res.Body)
return nil
}
示例2: writeBytes
// WriteBytes writes an encrypted/compressed stream to an io.Writer
func writeBytes(out io.Writer, key string, data []byte) error {
var gzWriter *gzip.Writer // compressed writer
var iv [aes.BlockSize]byte // initialization vector
var cb cipher.Block // cipher block interface
var err error // general error holder
// init cipher block
if cb, err = aes.NewCipher(hashKey(key)); err != nil {
return err
}
// init encrypted writer
encWriter := &cipher.StreamWriter{
S: cipher.NewOFB(cb, iv[:]),
W: out,
}
// close when done
defer encWriter.Close()
// wrap encrypted writer
gzWriter = gzip.NewWriter(encWriter)
// close when done
defer gzWriter.Close()
// copy data to destination file compressing and encrypting along the way
_, err = io.Copy(gzWriter, bytes.NewReader(data))
// return copy error
return err
}
示例3: NewDscout
func NewDscout(waiter *sync.WaitGroup, filename string) *Dscout {
d := new(Dscout)
d.Operation.Waiter = waiter
d.Operation.Waiter.Add(1)
file, err := os.Create(filename)
if err != nil {
return nil
}
d.closer = func() {
file.Close()
}
var writer io.WriteCloser = file
var compressor *gzip.Writer
if strings.HasSuffix(filename, ".gz") {
compressor = gzip.NewWriter(file)
d.closer = func() { compressor.Close(); file.Close() }
writer = compressor
}
uncompressed_name := strings.TrimRight(filename, ".gz")
switch {
case strings.HasSuffix(uncompressed_name, ".gob"):
d.marshaler = new(formats.GobMarshaler)
case strings.HasSuffix(uncompressed_name, ".xml"):
d.marshaler = new(formats.XmlMarshaler)
}
if d.marshaler != nil {
d.marshaler.InitFile(writer)
}
return d
}
示例4: CreateGobsFile
func CreateGobsFile(targetFilePath string, recs *[]interface{}, getRecPtr GobRecPtrMaker, gzipped bool) {
var file, err = os.Create(targetFilePath)
var gobber *gob.Encoder
var gzipper *gzip.Writer
if file != nil {
defer file.Close()
}
if err != nil {
panic(err)
}
if gzipped {
if gzipper, err = gzip.NewWriterLevel(file, gzip.BestCompression); gzipper != nil {
defer gzipper.Close()
gobber = gob.NewEncoder(gzipper)
}
if err != nil {
panic(err)
}
} else {
gobber = gob.NewEncoder(file)
}
for _, rec := range *recs {
if err = gobber.Encode(coreutil.PtrVal(getRecPtr(rec))); err != nil {
panic(err)
}
}
}
示例5: newGzipResponseWriter
func newGzipResponseWriter(w http.ResponseWriter) *gzipResponseWriter {
var gz *gzip.Writer
if gzI := gzipWriterPool.Get(); gzI == nil {
gz = gzip.NewWriter(w)
} else {
gz = gzI.(*gzip.Writer)
gz.Reset(w)
}
return &gzipResponseWriter{WriteCloser: gz, ResponseWriter: w}
}
示例6: Publish
func (this *HtmlView) Publish(ctxt *web.Context) (err error) {
names := mvc.GetMvcMeta(ctxt)
if names[mvc.MVC_ACTION] == "" {
names[mvc.MVC_ACTION] = "_"
}
var tmpl *template.Template
tmpl, err = this.getTmpl(names)
if err == nil {
var method = ctxt.Method()
ctxt.SetHeader("Content-Type", "text/html; charset=utf-8")
if method != "HEAD" {
var err error
var tw io.Writer = ctxt.Response
var gzipwriter *gzip.Writer
if ctxt.ReqHeaderHas("Accept-Encoding", "gzip") {
ctxt.SetHeader("Content-Encoding", "gzip")
gzipwriter, _ = gzip.NewWriterLevel(ctxt.Response, gzip.BestSpeed)
tw = gzipwriter
}
ctxt.SetHeader("Vary", "Accept-Encoding")
ctxt.Response.WriteHeader(200)
err = tmpl.Execute(tw, this.VM)
if err != nil {
// Header already sent... multiple write headers
//panic(err)
log.Println(err)
}
if gzipwriter != nil {
gzipwriter.Close()
}
if flushw, ok := ctxt.Response.(http.Flusher); ok {
flushw.Flush()
}
} else {
ctxt.Response.WriteHeader(200)
}
} else {
log.Println(err)
ctxt.SetErrorCode(500)
}
return
}
示例7: webQuitHandler
func webQuitHandler(diskwriter *csv.Writer, gzipwriter *gzip.Writer, csvfile *os.File) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "flushing to disk and shutting down")
diskwriter.Flush()
if gzipwriter != nil {
gzipwriter.Flush()
gzipwriter.Close()
}
csvfile.Close()
os.Exit(0)
}
}
示例8: main
func main() {
var file *os.File
var err error
var writer *gzip.Writer
var body []byte
if file, err = os.Create("output/sample.tar.gz"); err != nil {
log.Fatalln(err)
}
defer file.Close()
// gzip.NewWriter()なら、エラーを返さないので便利
if writer, err = gzip.NewWriterLevel(file, gzip.BestCompression); err != nil {
log.Fatalln(err)
}
defer writer.Close()
var filepaths = []string{
"files/b0044482_1413812.jpg",
"files/dart_flight_school.png",
"files/golang.txt",
}
// Write()がio.Writerと同じなので、そのまま行ける。
// 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()
}
示例9: serveSync
func (s *server) serveSync(rw http.ResponseWriter, req *http.Request) {
memberID := s.validateConnection(rw, req)
if memberID == "" {
return
}
s.logger.Infof("Peer Member %s started synchronization from address %s", memberID, req.RemoteAddr)
// Create a new channel for the new member synchronization
syncChan := make(chan []byte)
s.syncReqChannel <- syncChan
// Send the response header to client side
flusher := rw.(http.Flusher)
gzipped := strings.Contains(req.Header.Get("Accept-Encoding"), "gzip")
var gzipWriter *gzip.Writer
var err error
if gzipped {
// override the response writer to be a gzipped writer
if gzipWriter, err = gzip.NewWriterLevel(rw, gzip.DefaultCompression); err == nil {
rw = gzipResponseWrapper{Writer: gzipWriter, ResponseWriter: rw}
rw.Header().Set("Content-Encoding", "gzip")
flusher = rw.(http.Flusher)
defer func() {
gzipWriter.Close()
}()
} else {
s.logger.Warnf("Gzip wrapper creation for %s from %s failed: %v. Falling back to uncompressed HTTP", memberID, req.RemoteAddr, err)
}
}
encoder := newEncoder(rw)
var msgID uint64
for data := range syncChan {
ev := &sse{id: strconv.FormatUint(msgID, 10), event: "SYNC", data: string(data)}
// Write to the ResponseWriter
// Server Sent Events compatible
if err := encoder.Encode(ev); err != nil {
s.logger.WithFields(log.Fields{
"error": err,
}).Errorf("Failed to encode sync message to peer member %s", memberID)
return
}
msgID++
}
flusher.Flush()
s.logger.Infof("Peer Member %s synchronization has been completed successfully", memberID)
}
示例10: createInvoiceFile
func createInvoiceFile(filename string) (io.WriteCloser, func(), error) {
file, err := os.Create(filename)
if err != nil {
return nil, nil, err
}
closer := func() { file.Close() }
var writer io.WriteCloser = file
var compressor *gzip.Writer
if strings.HasSuffix(filename, ".gz") {
compressor = gzip.NewWriter(file)
closer = func() { compressor.Close(); file.Close() }
writer = compressor
}
return writer, closer, nil
}
示例11: getPubmedRecords
func getPubmedRecords(urlFetcher *gopubmed.Fetcher, first bool, meshWriter *gzip.Writer, xmlWriter *gzip.Writer, transport *http.Transport, pmids []string) {
preUrlTime := time.Now()
articles, raw, err := urlFetcher.GetArticlesAndRaw(pmids)
if err != nil {
log.Fatal(err)
}
s := string(raw[:len(raw)])
for i := 0; i < len(articles); i++ {
pubmedArticle := articles[i]
if pubmedArticle.MedlineCitation != nil && pubmedArticle.MedlineCitation.MeshHeadingList != nil && pubmedArticle.MedlineCitation.MeshHeadingList.MeshHeading != nil {
fmt.Fprint(meshWriter, articles[i].MedlineCitation.PMID.Text)
for j := 0; j < len(pubmedArticle.MedlineCitation.MeshHeadingList.MeshHeading); j++ {
fmt.Fprint(meshWriter, "|")
fmt.Fprint(meshWriter, pubmedArticle.MedlineCitation.MeshHeadingList.MeshHeading[j].DescriptorName.Attr_UI)
fmt.Fprint(meshWriter, "::"+pubmedArticle.MedlineCitation.MeshHeadingList.MeshHeading[j].DescriptorName.Text)
if len(pubmedArticle.MedlineCitation.MeshHeadingList.MeshHeading[j].QualifierName) > 0 {
fmt.Fprint(meshWriter, "=")
for q := 0; q < len(pubmedArticle.MedlineCitation.MeshHeadingList.MeshHeading[j].QualifierName); q++ {
if q != 0 {
fmt.Fprint(meshWriter, "&")
}
fmt.Fprint(meshWriter, pubmedArticle.MedlineCitation.MeshHeadingList.MeshHeading[j].QualifierName[q].Attr_UI)
fmt.Fprint(meshWriter, "::"+pubmedArticle.MedlineCitation.MeshHeadingList.MeshHeading[j].QualifierName[q].Text)
}
}
}
fmt.Fprintln(meshWriter, "")
}
}
meshWriter.Flush()
if !first {
s = strings.Replace(s, startXml, "", -1)
s = strings.Replace(s, docType, "", -1)
s = strings.Replace(s, startPubmedArticleSet, "", -1)
}
s = strings.Replace(s, endPubmedArticleSet, "<!-- breakset -->", -1)
xmlWriter.Write([]byte(s))
postUrlTime := time.Now()
log.Println("Total request time:", postUrlTime.Sub(preUrlTime))
}
示例12: gzip
func (e *Engine) gzip() error {
var buf bytes.Buffer
var level int
var err error
level, err = e.stack.PopInt()
if err == nil {
var w *gzip.Writer
w, err = gzip.NewWriterLevel(&buf, level)
if err == nil {
_, err = w.Write(e.stack.Pop())
w.Close()
}
}
if err == nil {
e.stack.Push(buf.Bytes())
}
return err
}
示例13: writeSure
func writeSure(path string, info DirWalker) (err error) {
file, err := os.Create(path)
if err != nil {
return
}
defer file.Close()
var zfile io.Writer
if strings.HasSuffix(path, ".gz") {
var tmp *gzip.Writer
tmp = gzip.NewWriter(file)
defer tmp.Close()
zfile = tmp
} else {
zfile = file
}
io.WriteString(zfile, magic)
dumpDir(zfile, "__root__", info)
return
}
示例14: NewFileWriter
func (nf *Netflow) NewFileWriter(filename string, mode int, compress bool) error {
log.Debugln("NewFileWriter")
if _, ok := nf.writers[filename]; ok {
return fmt.Errorf("netflow writer %v already exists", filename)
}
f, err := os.Create(filename)
if err != nil {
return err
}
c := make(chan *Packet, BUFFER_DEPTH)
go func() {
var w *gzip.Writer
if compress {
log.Debugln("using compression")
w = gzip.NewWriter(f)
}
for {
d := <-c
if d == nil {
break
}
if mode == ASCII {
if compress {
w.Write([]byte(d.GoString()))
} else {
f.Write([]byte(d.GoString()))
}
} else {
if compress {
w.Write(d.Raw)
} else {
f.Write(d.Raw)
}
}
}
if compress {
w.Close()
}
f.Close()
}()
nf.registerWriter(filename, c)
return nil
}
示例15: calcFileInfo
func calcFileInfo(fi *FileInfo) {
fmt.Printf("calcFileInfo: '%s'\n", fi.Path)
const BufSize = 16 * 1024
var buf [BufSize]byte
r, err := os.Open(fi.Path)
fataliferr(err)
defer r.Close()
sha1 := sha1.New()
md5Hash := md5.New()
fi.ShouldCompress = false
tryCompressFirsBlock := shouldTryCompressFile(fi.Path)
var gzw *gzip.Writer
compressedData := &bytes.Buffer{}
fi.Size = 0
fi.CompressedData = nil
for {
n, err := r.Read(buf[:])
if err == io.EOF {
break
}
d := buf[:n]
fataliferr(err)
fatalif(n == 0, "n is 0")
fi.Size += n
_, err = sha1.Write(d)
fataliferr(err)
_, err = md5Hash.Write(d)
fataliferr(err)
if tryCompressFirsBlock {
tryCompressFirsBlock = false
gz, err := gzip.NewWriterLevel(compressedData, gzip.BestCompression)
fataliferr(err)
_, err = gz.Write(d)
fataliferr(err)
gz.Close()
compressedSize := compressedData.Len()
saved := n - compressedSize
// relatively high threshold of 20% savings on compression
fi.ShouldCompress = saved > 0 && perc(compressedSize, saved) > 20
diff := n - compressedSize
fmt.Printf(" should compress: %v, %d => %d (%d %.2f%%)\n", fi.ShouldCompress, n, compressedSize, diff, perc(n, diff))
if fi.ShouldCompress {
compressedData = &bytes.Buffer{}
gzw, err = gzip.NewWriterLevel(compressedData, gzip.BestCompression)
fataliferr(err)
}
}
if gzw != nil {
_, err = gzw.Write(d)
fataliferr(err)
}
}
sha1Sum := sha1.Sum(nil)
fi.Sha1Hex = fmt.Sprintf("%x", sha1Sum)
if gzw != nil {
gzw.Close()
compressedSize := compressedData.Len()
// only use compressed if compressed by at least 5%
if compressedSize+(compressedSize/20) < fi.Size {
fi.CompressedData = compressedData.Bytes()
}
}
md5Sum := md5Hash.Sum(nil)
fi.Md5Hex = fmt.Sprintf("%x", md5Sum)
// if compressed, md5 is of the compressed content
if fi.CompressedData != nil {
md5Sum2 := md5.Sum(fi.CompressedData)
fi.Md5Hex = fmt.Sprintf("%x", md5Sum2[:])
}
fi.S3PathSha1Part = sha1HexToS3Path(fi.Sha1Hex)
ext := strings.ToLower(filepath.Ext(fi.Path))
if fi.CompressedData != nil {
fi.S3FullPath = fi.S3PathSha1Part + ".gz" + ext
} else {
fi.S3FullPath = fi.S3PathSha1Part + ext
}
fmt.Printf(" sha1: %s\n", fi.Sha1Hex)
fmt.Printf(" md5: %s\n", fi.Md5Hex)
fmt.Printf(" s3: %s\n", fi.S3FullPath)
fmt.Printf(" size: %d\n", fi.Size)
if fi.CompressedData != nil {
sizedCompressed := len(fi.CompressedData)
saved := fi.Size - sizedCompressed
fmt.Printf(" size compressed: %d (saves %d %.2f%%)\n", sizedCompressed, saved, perc(fi.Size, saved))
}
}