本文整理匯總了Golang中compress/gzip.Writer.Close方法的典型用法代碼示例。如果您正苦於以下問題:Golang Writer.Close方法的具體用法?Golang Writer.Close怎麽用?Golang Writer.Close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類compress/gzip.Writer
的用法示例。
在下文中一共展示了Writer.Close方法的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: 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)
}
}
}
示例4: 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
}
示例5: 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)
}
}
示例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: 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()
}
示例8: 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)
}
示例9: 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
}
示例10: 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
}
示例11: 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
}
示例12: 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
}
示例13: runPatchManifest
func runPatchManifest(args []string) (exit int) {
var fh *os.File
var err error
if patchReplace && patchOverwrite {
stderr("patch-manifest: Cannot use both --replace and --overwrite")
return 1
}
if !patchReplace && len(args) != 2 {
stderr("patch-manifest: Must provide input and output files (or use --replace)")
return 1
}
if patchReplace && len(args) != 1 {
stderr("patch-manifest: Must provide one file")
return 1
}
if patchManifestFile != "" && (patchName != "" || patchExec != "" || patchUser != "" || patchGroup != "" || patchCaps != "" || patchMounts != "") {
stderr("patch-manifest: --manifest is incompatible with other manifest editing options")
return 1
}
inputFile = args[0]
// Prepare output writer
if patchReplace {
fh, err = ioutil.TempFile(path.Dir(inputFile), ".actool-tmp."+path.Base(inputFile)+"-")
if err != nil {
stderr("patch-manifest: Cannot create temporary file: %v", err)
return 1
}
} else {
outputFile = args[1]
ext := filepath.Ext(outputFile)
if ext != schema.ACIExtension {
stderr("patch-manifest: Extension must be %s (given %s)", schema.ACIExtension, ext)
return 1
}
mode := os.O_CREATE | os.O_WRONLY
if patchOverwrite {
mode |= os.O_TRUNC
} else {
mode |= os.O_EXCL
}
fh, err = os.OpenFile(outputFile, mode, 0644)
if err != nil {
if os.IsExist(err) {
stderr("patch-manifest: Output file exists (try --overwrite)")
} else {
stderr("patch-manifest: Unable to open output %s: %v", outputFile, err)
}
return 1
}
}
var gw *gzip.Writer
var w io.WriteCloser = fh
if !patchNocompress {
gw = gzip.NewWriter(fh)
w = gw
}
tw := tar.NewWriter(w)
defer func() {
tw.Close()
if !patchNocompress {
gw.Close()
}
fh.Close()
if exit != 0 && !patchOverwrite {
os.Remove(fh.Name())
}
}()
// Prepare input reader
input, err := os.Open(inputFile)
if err != nil {
stderr("patch-manifest: Cannot open %s: %v", inputFile, err)
return 1
}
defer input.Close()
tr, err := aci.NewCompressedTarReader(input)
if err != nil {
stderr("patch-manifest: Cannot extract %s: %v", inputFile, err)
return 1
}
defer tr.Close()
var newManifest []byte
if patchManifestFile != "" {
mr, err := os.Open(patchManifestFile)
if err != nil {
stderr("patch-manifest: Cannot open %s: %v", patchManifestFile, err)
return 1
//.........這裏部分代碼省略.........
示例14: ReturnGZipWriter
// Closes a gzip writer and returns it to the pool:
func ReturnGZipWriter(gz *gzip.Writer) {
gz.Close()
gzipWriterCache.Put(gz)
}
示例15: main
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
tr := &http.Transport{
ResponseHeaderTimeout: time.Second * 500,
DisableKeepAlives: false,
DisableCompression: false,
}
var wXml *gzip.Writer = nil
var ww *bufio.Writer = nil
var xFile *os.File = nil
meshFile, err2 := os.Create(meshFile)
if err2 != nil {
return
}
defer meshFile.Close()
wwMesh := bufio.NewWriter(meshFile)
wMesh := gzip.NewWriter(wwMesh)
//w := bufio.NewWriter(file)
numIdsPerUrl := findNumIdsPerUrl()
pmids := make([]string, numIdsPerUrl)
urlFetcher := gopubmed.Fetcher{
Ssl: false,
Transport: &http.Transport{
ResponseHeaderTimeout: time.Second * 500,
DisableKeepAlives: false,
DisableCompression: false,
//TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
allCount := 0
count := 0
reader, err := makeReader()
if err != nil {
log.Fatal(err)
}
first := true
chunkCount := 0
for {
thisNumIdsPerUrl := findNumIdsPerUrl()
if numIdsPerUrl != thisNumIdsPerUrl {
numIdsPerUrl = thisNumIdsPerUrl
pmids = make([]string, numIdsPerUrl)
}
line, err := reader.ReadString('\n')
if err != nil {
// You may check here if err == io.EOF
break
}
line = strings.TrimSpace(line)
err = lineChecker(line)
if err != nil {
log.Fatal(err)
}
//log.Println(line)
pmids[count] = line
if wXml == nil {
wXml, ww, xFile = makeXmlWriter(allCount, pmids[0])
}
count = count + 1
// Collected enough pmids: get their XML from NIH
if count == numIdsPerUrl {
getPubmedRecords(&urlFetcher, first, wMesh, wXml, tr, pmids)
checkTime()
first = false
count = 0
zeroArray(pmids)
} else {
}
allCount += 1
chunkCount += 1
// Start new xml file: close old one: open new one
if chunkCount > recordsPerFile {
fmt.Fprintln(wXml, endPubmedArticleSet)
wXml.Flush()
wXml.Close()
ww.Flush()
wXml, ww, xFile = makeXmlWriter(allCount, pmids[0])
chunkCount = 0
first = true
}
if allCount%500 == 0 {
log.Println(allCount)
}
}
if count != 0 {
getPubmedRecords(&urlFetcher, first, wMesh, wXml, tr, pmids)
}
fmt.Fprintln(wXml, endPubmedArticleSet)
//.........這裏部分代碼省略.........