js解码base64字符串后,下载解压得到的zip文件

downloadBase64AsZip(base64_data, '加密文件_' + Date.now());

function downloadBase64AsZip(base64String, fileName) {
	// 1. 解码Base64字符串
	const binaryString = window.atob(base64String);
	const len = binaryString.length;
	const bytes = new Uint8Array(len);
	for (let i = 0; i < len; i++) {
		bytes[i] = binaryString.charCodeAt(i);
	}

	// 2. 创建Blob对象
	const blob = new Blob([bytes], { type: 'application/zip' });

	// 3. 创建下载链接并触发下载
	const url = URL.createObjectURL(blob);
	const a = document.createElement('a');
	a.href = url;
	a.download = fileName;
	document.body.appendChild(a);
	a.click();
	document.body.removeChild(a);

	// 4. 清理
	URL.revokeObjectURL(url);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要用Java的Base64类将Base64字符串解码成byte数组,然后将byte数组通过GZIP进行压缩,最后再把压缩后的byte数组通过Base64类编码成压缩后的Base64字符串。以下是示例代码: ```java import java.util.zip.Deflater; import java.util.zip.Inflater; import java.util.Base64; public class Base64Compression { public static String compress(String base64String) { byte[] decodedBytes = Base64.getDecoder().decode(base64String); byte[] compressedBytes = compressBytes(decodedBytes); return Base64.getEncoder().encodeToString(compressedBytes); } public static String decompress(String compressedBase64String) { byte[] compressedBytes = Base64.getDecoder().decode(compressedBase64String); byte[] decompressedBytes = decompressBytes(compressedBytes); return Base64.getEncoder().encodeToString(decompressedBytes); } private static byte[] compressBytes(byte[] data) { Deflater deflater = new Deflater(); deflater.setInput(data); deflater.finish(); byte[] buffer = new byte[data.length]; int compressedSize = deflater.deflate(buffer); byte[] compressedData = new byte[compressedSize]; System.arraycopy(buffer, 0, compressedData, 0, compressedSize); return compressedData; } private static byte[] decompressBytes(byte[] data) { Inflater inflater = new Inflater(); inflater.setInput(data); byte[] buffer = new byte[data.length * 2]; int decompressedSize; try { decompressedSize = inflater.inflate(buffer); } catch (Exception e) { throw new RuntimeException("Failed to decompress data", e); } byte[] decompressedData = new byte[decompressedSize]; System.arraycopy(buffer, 0, decompressedData, 0, decompressedSize); return decompressedData; } } ``` 可以使用以下代码测试: ```java String base64String = "SGVsbG8gV29ybGQh"; String compressedBase64String = Base64Compression.compress(base64String); System.out.println(compressedBase64String); // 输出: "eJwrSS0uyczPAQAAP//I1g==" String decompressedBase64String = Base64Compression.decompress(compressedBase64String); System.out.println(decompressedBase64String); // 输出: "SGVsbG8gV29ybGQh" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值