zip.js 实现前端解压 zip字符串

项目需求

H5 App要求离线更新数据(不会有接口提供).由于数据不是很大.考虑用类似注册码的方式.获得一串json数据体后按用户对称加密再zip压缩转base64分发. 由用户手动复制字符串黏贴到应用输入框中进行更新.

简要实现方式

由java端进行 json数据体的 加密->zip->base64
由页面端进行解密

页面端解密方式

使用 zipJS


<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script type="text/javascript" src="zip.js">script>
  <title>Documenttitle>
head>
<body>
<script>
var zz='zip后的的base64字符串  ==';  // 要替换这里


//解压base64 zip字符流
function unzipString(base64,cb){
  zip.createReader(new zip.Data64URIReader(base64), function(reader) {
  reader.getEntries(function(entries) {
    if (entries.length) {
      entries[0].getData(new zip.TextWriter(), function(text) {
        cb(text);
        reader.close(function() {
        });
      }, function(current, total) {
      });
    }
  });
  }, function(error) {
  });
}

String.prototype.replaceAll  = function(s1,s2){
    return this.replace(new RegExp(s1,"gm"),s2);
}
unzipString(zz,function(retVal){
  console.log(retVal.toString());
});

script>
body>
html>


你可能感兴趣的:(Html小技巧)