首页 > 其他分享 >记:后端对字符串进行gzip压缩,前端js进行gzip解压

记:后端对字符串进行gzip压缩,前端js进行gzip解压

时间:2022-12-29 15:58:44浏览次数:46  
标签:解压 js let gzip new byte data out

最近有个需求要求对长字符串进行gzip压缩,然后在js进行解压缩的操作:

public static void main(String[] args) {
try {
String longString = "www.baidu.com";
// GZIP压缩后的数据
byte[] compress = compress(longString.getBytes());
//通过Base64转成字符串
String longStringEncoded = Base64.getEncoder().encodeToString(compress);
} catch (IOException e) {
e.printStackTrace();
}
}

public static byte[] compress(byte[] data) throws IOException {
if (data == null || data.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(data);
gzip.close();
return out.toByteArray();
}

public static byte[] uncompress(byte[] data) throws IOException {
if (data == null || data.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
GZIPInputStream gzip = new GZIPInputStream(inputStream);
byte[] buffer = new byte[256];
int n;
while ((n = gzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
gzip.close();
inputStream.close();
return out.toByteArray();
}
//后端压缩后的字符串
let encodeDpUrl = 'H4sIAAAAAAAAACsvL9dLSsxMKdVLzs8FAA3FGxcNAAAA';
//Base64解码
let gzipUrl = atob(encodeDpUrl);
// 将二进制字符串转换为字符数字数组
let charData = gzipUrl.split('').map(function (x) { return x.charCodeAt(0); });
//将数字数组转换为字节数组
let binData = new Uint8Array(charData);
//unzip   需要引入 pako.js文件   https://github.com/nodeca/pako
var data = pako.inflate(binData);
// 将字节数组转字符串
let longString = String.fromCharCode.apply(null, new Uint16Array(data));
console.info(longString);

 

记:url编码解码问题
//后端进行url编码,
String encodeUrl = URLEncoder.encode(要编码的URL, Constant.ENCODING_UTF_8);
// 前端进行url解码
let decodeURI = decodeURIComponent(要解码的URL);

 

标签:解压,js,let,gzip,new,byte,data,out
From: https://www.cnblogs.com/zsw-wkx/p/17012712.html

相关文章

  • JSX/TSX的好处
    1.之前没怎么用过JSX/TSX,基本上还是用html/css/js分离的方式,但是最近的一个@click="func(3)"的实现,发现了JSX的好处之一;如上,在html的某个元素里用了@click="func(3)"属......
  • 解决使用JSON.stringify时遇到的循环引用问题
    利用 js MessageChannel函数进行封装一个函数新建一个MessageChannel的实例,获取两个管道,我们从管道1发送数据,从管道2进行接收,我们这样就拿到新的数据,这样就能......
  • js. hw4
    functionensure(condition,message){//在条件不成立的时候,输出messageif(!condition){console.log('***测试失败:',message)}else{con......
  • nestjs 使用异常过滤器
    创建过滤器需要实现ExceptionFilter然后实现他的catch方法最后通过response.status(status).json返回(response通过host.switchToHttp().getResponse()获取)在局部使用则......
  • js 实现版本号排序
    //方法一:从左到右迭代,从高位判断,返回高位的大小结果注意:仅适用于版本号各个位的位数相同letversions=["1.45.0","1.5","6","2.3.4.5"];versions=versions.sor......
  • vue+nuxtJs+monaco制作Monaco Editor编辑器
    目录前言一、版本二、使用前配置nuxt.config.js三、使用四、附录1.kind提示图标类型2.默认action前言使用版本较低一、版本二、使用前配置nuxt.config.js实现下方......
  • [JZOJ3864]【JSOI2014】歌剧表演
    DescriptionSolution这题非常有意思。本来我想各种二进制搞一波,但我看到数据后我放弃了。。。其实这题十分的水。我们把目前分辨不出的放在同一集合。那么对于演出操作,就......
  • 随手记(五):js函数参数默认值+热更新失效问题+常见操作符
    1.js函数参数ES5写法如果函数在调用时未提供隐式参数,参数会默认设置为: undefinedfunctionmyFunction(x,y){y=y||0;}ES6写法functionmyFunction(x,......
  • 在前端js worker里使用dom并且加载jquery
    四个工具:nodejs+npmnpm安装的jsdomnpm安装的jquerynpm安装的browserify网址:browserify:https://browserify.org/jsdom:https://github.com/jsdom/jsdom/安......
  • js判断是否为空对象
    一、js判断是否为空对象https://www.cnblogs.com/sefaultment/p/9444345.html方案1:vardata={};varb=JSON.stringify(data)=="{}";alert(b);//true方案......