欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

go语言通过zlib压缩数据的方法

程序员文章站 2022-05-26 09:40:25
本文实例讲述了go语言通过zlib压缩数据的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:package main import (  ...

本文实例讲述了go语言通过zlib压缩数据的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
package main
import (
    "fmt"
    "compress/zlib"
    "bytes"
)
func main() {
    var input = []byte("data:image/png;base64,ivborw0kggoaaaansuheugaaaksaaaagcayaaabtn4gcaaai9kleqvr4xu2cfyxcvrmhn3pu3nm6lxa2ta0aylsgfsggthmu0w5hgkqapngark3cgm3brn3fygucisrbxanphcskg2i3hn9gy2gsnwq32aoe1c6krihfti2qsgvstmm783hvoa+zm69css8tacsq7n2sx87cj5p7zvls2ztnbseicg8hmjisbxmymnioxhja8fjnriuymndjosivnhd0y0wbrbxh6nhz16oduot2ixndc3/2mzjtoimonphj2p4xr13zkdtyjlhrwrgw4l1gaptifzt87aosuzxi/bqj3kz/xl1isglxd9aoh8u81wrksh5cefmuefubgmbabheeeziwbruvvccxaywasow419my17zw4evrz3ls9zbekjiywv+4bpkmivyvctnqps6bta2uahjzxqs4jzihxp7fxs1i7b947tjln5/7xbn9tien37ynn0exme6lecaegfr/746zjeubsy9ruvsigucxsypayuxuzotlfqenig8js+xratw+d/+y97p46sf7ymg4xblupw/zguqu1+ce4y0m8oas0uyopjmqqarjrydz4fvu1wrj6959s5ftwjl29dzolyxm1ut/eu3h7tejw2/+ywe683kipbjytgzbgkwjafdvxrfizpxsktwj3hbotheurq1hdzaelxmqzgsycmzav5xt+pndr/jg3hqzcjmm9acsijhqdlbwisxyaajud0ptuetkmyohwq9buwqdykrgz8kmxljlyzkryxrdz1cxgb5jdb/4xlty8opnmrc78tyildmwbrjicuxn6xg/ywgxwiwxzm4bouuqph8x9thvsyfvrjpj8nrysojihdjtv/2dfucz9mwabgckjiwaw8dgf3j+ggyi5ypzklbcz+8hsrtyqhpqy1qhaqnawwan0q/nrwlgdfvngta9uetzqueui2fej4+shp6vc9frdaidm0pwxprgaplq4k5+v5znqkeoqi5scgucwb2av756kzv+xuxqau8+pclrfdswzz7pvrcmcoar2wmppdcumxc6gtc35lvcc4p3pgfytdpdwhyvrevgtiupqsgkqihb9phyihebetfynrja0kxh7kdrxpl59cf7gfzj9a+ckkqzrkml6gigdfynkamqw3t/snatx9iu9y6lvydmqjxjjvrljjfvyiuh4fer53px46+ycllonddpro6pm/61bzd48dgeqxggrm5iaoefvcfrphu4wvorvsv5s1+mi/o1b4xz0ubw+1wgeedglk6yjl1jzx1gusf2a0uvysblxr9ez1wkbp0qxnovsqflayzpegn6b2q4ozbuo8lfn3ldjspkseevr1gxiyrpxrq5wu/gmffsgeg3curnvomdl5/b8thmol5j1i293dc6wzhjifrdua3gtfyjjjdsaarwiykfjxh+fil6/g/tfm+6saekmugrfd1oyxaytt+v8ikp4q9kla3fqhon19qslkvaosn7bklyz4kmpnrb1oumpsqy7fs3yx5keaga8dr2xloipxmfd/5sds85kyeeo8zk3tfzixujmkw0utxuyir1md8domrbyjm48clseahbfeinqopwm1zkhygduvtmnpk3ryodieqypdtrpjogjpotje+pt9sufpv4queuzsg9kycqfplsum4h73tynvvhiyqrw3khciybnvhrvebp7o+dedlp1vki5kjecga8ob0kjngw9nkcoutfpotqhe3fk55epwok0f496de0o2smyjqsnggghkmrfvlxox0snjazo+roe1qv1qaybjw1gu+lvsogljredn4b/keyebtddwqypt2jm0jwncdeykzaqpiogcdlc1i1ada1eg0xbetwyraqkwjaxcxjnamjekx/funrqmlwlidjpgqdyb6+bsrrfiekgmylcw/rvzhkxidmvituqluq9anuwqlvmaxn7a9w9lzlsvmvinw/phdvz6dsbn9px1kml/r7wpz1lj2mbjaqyxelkzwwnrkdojauye/zm8imz36awfc4pnmsmwp/giutmfbvbrv+avlj30ovvzzfxcfudra/h+sg//l910j7t9i6rqvntlmtjf0dywt1ebskeiqw8caoymohh+wl3omm9hrzki+phzvste3hhr2oe76wnil/kbnkr1aug4ikguuacxa1vy/ttcx0srz1dcsyckpqwrau6e9tzx1o3pnuybtm1tjvleoujvg2wt71t81srw5x761f7uvrpvfac+lrzlhvjffeuutb/t9iqax/bfg9t/g4ii5comqii+0qb8ynwjhkkbro3q+ipjrntwlre0hyrtvsswpgrm+zdtbr9yras8c41rmden0uzzhvsea+tl181zzvqyagx+jdvszneaughn2v/kgz985etoeitsqjww8c45k/jrhxrkeym49f/0+/qkj1lqhrptf07msplexaluucl3yvtkmtdn7pyvclxacppjwbmf3olxdsaeqadshayyj/5xhra4wxdp+4vlab+3kdthdmmti8aqqlbwnixdwfuok5uma5c9ltdaqemc1rx+uooajqnupkwpaxxxl2mn1leegkycgeedz55zhyx+6+ai34b8aiatbtejyatcugbpfcb46b3rofvbvelmmhqvxt1lgauq2expvg7bmfflbmkzgryao89jud2z66+crn4unnoqsdyeiwotcbwvgqk4gqhq4+40xo66wilkc8xamolgqiaih1sipvypostz+zjdncriar8kjvg30f+egvhlh0hpel6ddy0gby0hqwcxiqwchinu/nfzg4lsd700iajwncpabez1gv5ihqdvp5n9vbx2kli5nveftrd/zd9v2vu+jitoy1u8jlbsksdlgxct1dbwmjkfrn4p3bxqfxs6noxzadm2uxft77f1czp/kdvhrl7+wugmomigd6cqehamsqrbfnktw6/yawdhnkeee2etp1esglfrd9cu95wn6rjsotvefd31lrmjyea80ky2n+fnad817i3fymciinlmjo780u8qxdcvntp//ngziywu8jgrnzrwhmzpwt5i+pd2qc5ikaaaaasuvork5cyii")
    var buf bytes.buffer
    compressor, err := zlib.newwriterleveldict(&buf, zlib.bestcompression, input)
    if err != nil {
        fmt.println("压缩失败")
        return
    }
    compressor.write(input)
    compressor.close()
    fmt.println(buf.bytes())
    fmt.println(len(buf.bytes()))
    fmt.println(len(input))
}

希望本文所述对大家的go语言程序设计有所帮助。