This is a creation in Article, where the information may have evolved or changed.
In Python it is customary to use zlib for Web page compression. Golang also uses zlib for compression decompression. Zlib official gives the method is very simple, here right when a supplement.
articles are often modified and updated. Annotated under Golang zlib original address, http://xiaorui.cc/?p=3084
Zlib. Newwriter () can only pass []byte type data. The newwriterlevel can pass the compression level.
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">#xiaorui. Ccpackage mainimport ("bytes" "Compress/zlib" "FMT" "IO") func main () {var in bytes. Bufferb: = []byte (' xiorui.cc ') w: = Zlib. Newwriter (&in) w.write (b) W.close () var out bytes. Bufferr, _: = Zlib. Newreader (&in) io. Copy (&out, R) fmt. Println (out). String ())}</textarea>
123456789101112131415161718192021222324 |
#xiaorui. CC PackageMainImport ("bytes""Compress/zlib""FMT""IO")funcMain() {varinch bytes.Bufferb := []byte(`Xiorui.cc`)W := zlib.Newwriter(&inch)W.Write(b)W.Close() var outbytes.BufferR, _ := zlib.Newreader(&inch)io.Copy(& out, R)FMT.Println( out.String())} |
Library Package Address
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">import "Compress/zlib"</textarea>
12 |
Import "Compress/zlib" |
Write
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">func newwriter func newwriter (w io). Writer) *writer</textarea>
1234 |
func Newwriter func newwriter( w io. Writer ) *Writer |
To read compressed data
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">func newreader func newreader (r io. Reader) (IO. Readcloser, error)</textarea>
1234 |
func Newreader func newreader( r io. Reader ) (io. Readcloser , error) |
Set the compression level and compress the data
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">func newwriterlevel (w io. Writer, Level int) (IO. Writecloser, OS. Error)</textarea>
12 |
func newwriterlevel Span class= "Crayon-sy" > ( w io writer level int ( io writecloser os error ) |
Here are a few levels.
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">Const (nocompression = 0 Bestspeed = 1 bestcompression = 9 Defaultcompression = 1 ) const (nocompression = Flate. Nocompression bestspeed = flate. Bestspeed bestcompression = flate. Bestcompression defaultcompression = flate. Defaultcompression)</textarea>
123456789101112131415 |
Const ( nocompression = 0 Bestspeed = 1 bestcompression = 9 defaultcompression = -1)Const ( nocompression = flate.nocompression Bestspeed = flate.Bestspeed bestcompression = flate.bestcompression defaultcompression = flate.defaultcompression) |
Write Data
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">func (*writer) write func (z *writer) write (P []byte) (n int, err error)</textarea>
1234 |
func (*Writer) Write func (Z *Writer) Write(P []byte) (N int, ErrError) |
Shut down
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">func (*writer) close func (z *writer) Close () error</textarea>
1234 |
func ( * writer ) close func ( z * writer ) Span class= "crayon-e" >close ( error |
Goang zlib compression efficiency and performance are also possible, after all, rsync is also using this compression algorithm. in fact zlib and gzip contrast, seemingly with gzip a bit more. For example, Nginx gzip compression. Previously read a foreign post, is said zlib than gzip more suitable for that speed and compression effect of the scene.