This commit is contained in:
linfso 2025-07-10 18:08:12 +08:00
parent f989fbe810
commit 4f8b4cb8f5

37
src/utils/compress.js Normal file
View File

@ -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+'');
}