1.引入必须依赖库jszip+jsutils=>>>建议使用以下版本,其他版本的jszip会报错
JSZipUtils.getBinaryContent('read.zip', function (err, data) {
if (err) {
throw err; // or handle err
}
var zip = new JSZip(data);
function Uint8ArrayToString(fileData){
var dataString = "";
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i]);
}
return dataString
}
var dirname = 'read';
// 转换格式字符串
var str = Uint8ArrayToString(zip.folder().files[dirname+'/package.json']._data.getContent());
//将字符串转换对象
var res = JSON.parse(str);
console.log(res)
function Uint8ArrayToString(fileData){
var dataString = "";
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i]);
}
return dataString
}
字符串转Uint8Array
function stringToUint8Array(str){
var arr = [];
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
var tmpUint8Array = new Uint8Array(arr);
return tmpUint8Array
}
string转ArrayBuffer
function str2ab(str) {
var buf = new ArrayBuffer(str.length * 2); // 每个字符占用2个字节
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
ArrayBuffer转String
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}