[WeChat Mini Program] Introducing the third-party library poke to decompress GZIP compressed data

  1. Use the npm package management tool:

    • npm initFirst, execute the initialization project in the root directory of the applet to generate package.jsonfiles.
    • Then, install pako via npm: npm install pako.
    • miniprogram_npmNext, create a folder named in the root directory of the applet .
    • Copy node_modules/pakothe folder into miniprogram_npmthe folder.
    • Use to introduce pako in the js file in the page or component that needs to use require()pako:
    const pako = require('pako');
    
  2. Introduced through CDN:

    • Open project.config.jsonthe file, add the following configuration, and specify the third-party libraries that need to be introduced:
    {
          
          
      "setting": {
          
          
        "urlCheck": true,
        "es6": true,
        "postcss": true,
        "minified": true,
        "newFeature": true
      },
      "packOptions": {
          
          
        "ignore": []
      },
      "compileType": "miniprogram",
      "libVersion": "2.14.4",
      "appid": "your appid",
      "projectname": "your projectname",
      "debugOptions": {
          
          
        "hidedInDevtools": []
      },
      "cdnUrl": {
          
          
        "default": "https://unpkg.com/"
      },
      "usingComponents": {
          
          }
    }
    
    • Use to introduce pako in the js file in the page or component that needs to use importpako:
    import pako from 'pako';
    

Whether you use the npm package management tool or introduce it through CDN, you need to build it in the mini program development tool to ensure that the introduced third-party library can be correctly identified and used.

Please note that the environment of the WeChat applet is different from the browser environment, and some additional adaptation work may be required to ensure that the pako library runs normally in the applet.


example

Convert string "{\"data\":\"[31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -115, -112, 57, 14, -61, 48, 12, 4, -1, -62, 90, 16, -42, 36, 69, 29, 95, 9, 92, -91, 73, -31, -46, 93, -32, -65, -121, -115, 3, 75, 70, 28, -9, -125, -63, -20, 62, -34, -12, -92, 54, 89, 69, 22, -51, 12, -28, 20, -24, 69, 77, 37, 74, -96, -123, 26, 34, 2, -83, -44, -72, -60, -76, -123, -114, 46, -128, -44, -99, 78, 35, 109, 61, 93, -44, 105, -34, 105, -5, 67, 87, 0, 92, -18, -106, 84, 115, -73, -2, 44, -23, -23, 4, -17, -74, -17, 74, -67, 46, 73, -109, 119, 103, -69, -21, -10, 3, 15, -97, -100, -70, 117, -96, -19, -14, -63, -63, 45, 124, 124, -16, -28, -50, -37, -4, 1, 4, -126, -5, 117, -51, 1, 0, 0]\",\"total\":10}"to object

  • And []convert the part into a byte array (byte array), and then use the GZIP algorithm to decompress, follow the following steps:
  1. First, JSON.parse()convert the string to an object using the method:
const jsonString = "{\"data\":\"[31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -115, -112, 57, 14, -61, 48, 12, 4, -1, -62, 90, 16, -42, 36, 69, 29, 95, 9, 92, -91, 73, -31, -46, 93, -32, -65, -121, -115, 3, 75, 70, 28, -9, -125, -63, -20, 62, -34, -12, -92, 54, 89, 69, 22, -51, 12, -28, 20, -24, 69, 77, 37, 74, -96, -123, 26, 34, 2, -83, -44, -72, -60, -76, -123, -114, 46, -128, -44, -99, 78, 35, 109, 61, 93, -44, 105, -34, 105, -5, 67, 87, 0, 92, -18, -106, 84, 115, -73, -2, 44, -23, -23, 4, -17, -74, -17, 74, -67, 46, 73, -109, 119, 103, -69, -21, -10, 3, 15, -97, -100, -70, 117, -96, -19, -14, -63, -63, 45, 124, 124, -16, -28, -50, -37, -4, 1, 4, -126, -5, 117, -51, 1, 0, 0]\",\"total\":10}";
const obj = JSON.parse(jsonString);
  1. Next, convert obj.datathe field's values ​​into an array:
const dataArray = JSON.parse(obj.data);
  1. Then, create an Uint8Arrayobject and dataArraypass it the value of as argument to convert the array to an unsigned 8-bit integer array:
const uint8Array = new Uint8Array(dataArray);
  1. pakoFinally, decompression is performed using the library, a JavaScript library that handles GZIP compression and decompression. You can pako.inflate()unpack uint8Arraythe object using the method:
const inflatedArray = pako.inflate(uint8Array);

Please make sure pakoto include the library in your project before using it.

After the above steps are completed, inflatedArraythe decompressed byte array data will be included.

Guess you like

Origin blog.csdn.net/gao511147456/article/details/132019991