首页 技术 正文
技术 2022年11月18日
0 收藏 818 点赞 3,179 浏览 1980 个字
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; /**
*
* 对字符串进行加解密和加解压
*
*/
@SuppressWarnings("restriction")
public class Base64Util { private static Logger log = LoggerFactory.getLogger(Base64Util.class); /**
* 将字符串压缩后Base64
* @param primStr 待加压加密函数
* @return
*/
public static String encodeString(String primStr) {
if (primStr == null || primStr.length() == 0) {
return primStr;
}
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try{
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(primStr.getBytes("UTF-8"));
zout.closeEntry();
return new BASE64Encoder().encode(out.toByteArray());
} catch (IOException e) {
log.error("对字符串进行加压加密操作失败:", e);
return null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
log.error("对字符串进行加压加密操作,关闭zip操作流失败:", e);
}
}
}
} /**
* 将压缩并Base64后的字符串进行解密解压
* @param compressedStr 待解密解压字符串
* @return
*/
public static final String decodeString(String compressedStr) {
if (compressedStr == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed = null;
try {
byte[] compressed = new BASE64Decoder().decodeBuffer(compressedStr);
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString("UTF-8");
} catch (IOException e) {
log.error("对字符串进行解密解压操作失败:", e);
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
log.error("对字符串进行解密解压操作,关闭压缩流失败:", e);
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
log.error("对字符串进行解密解压操作,关闭输入流失败:", e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.error("对字符串进行解密解压操作,关闭输出流失败:", e);
}
}
}
return decompressed;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844