How can I use gzip on a string?

huangapple go评论84阅读模式
英文:

How can I use gzip on a string?

问题

我想使用Go语言从文件中读取一块内容,将其视为字符串,并对该块内容进行gzip压缩。我知道如何从文件中读取并将其视为字符串,但是在使用compress/gzip时感到困惑。

我应该创建一个io.Writer,将其写入一个buf(字节切片),使用gzip.NewWriter(io.Writer)获取一个w *gzip.Writer,然后使用w.Write(chunk_of_file)将文件块写入buf吗?然后我需要将字符串视为字节切片。

英文:

I want to use Go to read out a chunk from a file, treat it as a string and gzip this chunk. I know how to read from the file and treat it as a string, but when it comes to compress/gzip I am lost.

Should I create an io.writer, which writes to a buf (byte slice), use gzip.NewWriter(io.writer) to get a w *gzip.Writer and then use w.Write(chunk_of_file) to write the chunk of file to buf? Then I would need to treat the string as a byte slice.

答案1

得分: 37

你可以使用gzip.Writer来编写代码,因为它实现了io.Writer接口。

示例:

package main

import (
	"bytes"
	"compress/gzip"
	"fmt"
	"log"
)

func main() {
	var b bytes.Buffer
	gz := gzip.NewWriter(&b)
	if _, err := gz.Write([]byte("YourDataHere")); err != nil {
		log.Fatal(err)
	}
	if err := gz.Close(); err != nil {
		log.Fatal(err)
	}
	fmt.Println(b.Bytes())
}

Go Playground

如果你想设置压缩级别(默认为compress/flate中的-1),你可以使用gzip.NewWriterLevel

英文:

You can just write using gzip.Writer as it implements io.Writer.

Example:

package main

import (
	"bytes"
	"compress/gzip"
	"fmt"
	"log"
)

func main() {
	var b bytes.Buffer
	gz := gzip.NewWriter(&b)
	if _, err := gz.Write([]byte("YourDataHere")); err != nil {
		log.Fatal(err)
	}
	if err := gz.Close(); err != nil {
		log.Fatal(err)
	}
	fmt.Println(b.Bytes())
}

Go Playground

If you want to set the compression level (Default is -1 from compress/flate) you can use gzip.NewWriterLevel.

答案2

得分: 0

如果结果不需要返回到文件中,你可以直接使用Flate。这样可以减少使用Gzip时的一些开销。另一个选择是Brotli。以下是示例代码:

package main

import (
   "bytes"
   "compress/flate"
   "github.com/andybalholm/brotli"
)

func compressFlate(data []byte) ([]byte, error) {
   var b bytes.Buffer
   w, err := flate.NewWriter(&b, 9)
   if err != nil {
      return nil, err
   }
   w.Write(data)
   w.Close()
   return b.Bytes(), nil
}

func compressBrotli(data []byte) []byte {
   var b bytes.Buffer
   w := brotli.NewWriterLevel(&b, brotli.BestCompression)
   w.Write(data)
   w.Close()
   return b.Bytes()
}

结果代码:

package main

import (
   "bytes"
   "fmt"
)

func main() {
   data := bytes.Repeat([]byte("hello world"), 999_999)
   f, err := compressFlate(data)
   if err != nil {
      panic(err)
   }
   b := compressBrotli(data)
   fmt.Println(len(f) == 21379, len(b) == 40)
}

你可以在以下链接中查看更多信息:

英文:

If the result is not going back into a file, then you could just use Flate
directly. You save a bit of overhead from Gzip. Another option is Brotli.
Examples:

package main

import (
   "bytes"
   "compress/flate"
   "github.com/andybalholm/brotli"
)

func compressFlate(data []byte) ([]byte, error) {
   var b bytes.Buffer
   w, err := flate.NewWriter(&b, 9)
   if err != nil {
      return nil, err
   }
   w.Write(data)
   w.Close()
   return b.Bytes(), nil
}

func compressBrotli(data []byte) []byte {
   var b bytes.Buffer
   w := brotli.NewWriterLevel(&b, brotli.BestCompression)
   w.Write(data)
   w.Close()
   return b.Bytes()
}

Result:

package main

import (
   "bytes"
   "fmt"
)

func main() {
   data := bytes.Repeat([]byte("hello world"), 999_999)
   f, err := compressFlate(data)
   if err != nil {
      panic(err)
   }
   b := compressBrotli(data)
   fmt.Println(len(f) == 21379, len(b) == 40)
}

huangapple
  • 本文由 发表于 2013年10月5日 20:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/19197874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定