爱意满满的作品展示区。
iqoo

10 行 JS 代码实现 gzip 压缩/解压缩

  •  
  •   iqoo · Dec 8, 2021 · 3012 views
    This topic created in 1616 days ago, the information mentioned may be changed or developed.

    Chrome80+ 支持原生的 gzip 压缩 /解压缩,支持流模式。一个简单的用法:

    function gzipImpl(cls, algo, data) {
      const {writable, readable} = new cls(algo)
      const writer = writable.getWriter()
      writer
        .write(data)
        .then(_ => writer.close())
      return new Response(readable).arrayBuffer()
    }
    const gzip = gzipImpl.bind(0, CompressionStream, 'gzip')
    const gunzip = gzipImpl.bind(0, DecompressionStream, 'gzip')
    
    // test
    const srcData = new Uint8Array(10000)
    const zipData = await gzip(srcData)
    const unzipData = await gunzip(zipData)
    
    // src: 10000 zip: 45
    console.log('src:', srcData.length, 'zip:', zipData.byteLength)
    console.assert(srcData.length === unzipData.byteLength)
    
    7 replies    2021-12-09 19:32:03 +08:00
    EPr2hh6LADQWqRVH
        1
    EPr2hh6LADQWqRVH  
       Dec 8, 2021
    迷惑行为,http 这层压缩不就完了么,在里面套娃浪费 CPU 干什么
    iqoo
        2
    iqoo  
    OP
       Dec 8, 2021
    @avastms 上传呢
    hingbong
        3
    hingbong  
       Dec 8, 2021 via Android
    10 行实现 gzip 压缩算法调用啊,还以为能 10 行实现 gzip
    steptodream
        4
    steptodream  
       Dec 9, 2021
    抖音上 5 行 py 代码实现坦克大战
    liuidetmks
        5
    liuidetmks  
       Dec 9, 2021
    @avastms 有时候参数是加密的,需要先 gzip 再加密.
    liweiliang
        6
    liweiliang  
       Dec 9, 2021
    @steptodream 哈哈给整笑了
    newmlp
        7
    newmlp  
       Dec 9, 2021
    10 行代码完成 gzip 接口调用,我还以为能把整个压缩算法写出啦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3200 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 14:29 · PVG 22:29 · LAX 07:29 · JFK 10:29
    ♥ Do have faith in what you're doing.