-
Use the npm package management tool:
npm init
First, execute the initialization project in the root directory of the applet to generatepackage.json
files.- Then, install pako via npm:
npm install pako
. miniprogram_npm
Next, create a folder named in the root directory of the applet .- Copy
node_modules/pako
the folder intominiprogram_npm
the folder. - Use to introduce pako in the js file in the page or component that needs to use
require()
pako:
const pako = require('pako');
-
Introduced through CDN:
- Open
project.config.json
the 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
import
pako:
import pako from 'pako';
- Open
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:
- 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);
- Next, convert
obj.data
the field's values into an array:
const dataArray = JSON.parse(obj.data);
- Then, create an
Uint8Array
object anddataArray
pass it the value of as argument to convert the array to an unsigned 8-bit integer array:
const uint8Array = new Uint8Array(dataArray);
pako
Finally, decompression is performed using the library, a JavaScript library that handles GZIP compression and decompression. You canpako.inflate()
unpackuint8Array
the object using the method:
const inflatedArray = pako.inflate(uint8Array);
Please make sure pako
to include the library in your project before using it.
After the above steps are completed, inflatedArray
the decompressed byte array data will be included.