diff --git a/src/utils/compress.js b/src/utils/compress.js new file mode 100644 index 00000000..ea3894e8 --- /dev/null +++ b/src/utils/compress.js @@ -0,0 +1,37 @@ +// 导入pako +import { base64 } from 'js-md5' +import pako from 'pako' + +/** + * gzip 解压缩 + * @param {*} b64Data + * @returns + */ +export function unzip (b64Data) { + let strData = atob(b64Data) + // Convert binary string to character-number array + const charData = strData.split('').map(function (x) { return x.charCodeAt(0) }) + // Turn number array into byte-array + const binData = new Uint8Array(charData) + // // unzip + const decompressed = pako.inflate(binData) + return new TextDecoder('utf-8').decode(decompressed); +} + +/** + * gzip 压缩 + * @param {*} str + * @returns + */ +export function zip (str) { + if (typeof str !== 'string') { + str = JSON.stringify(str) + } + const binaryString = pako.gzip(str, { to: 'string' }) + return btoa(binaryString) +} + +export function unzipToJson(b64Data) { + const decStr=unzip(b64Data) + return eval(''+decStr+''); +}