Java中ZIP与Gzip的压缩与解压处理,其中有Base64处理
来源:互联网 发布:淘宝网零钱包 编辑:程序博客网 时间:2024/05/17 21:52
1、ZIP压缩与解压
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;import com.fusion.areain.common.Constants;/** * * 对字符串进行加解密和加解压 * @author *** * */@SuppressWarnings("restriction")public class ZipUtil {private static Logger log = LoggerFactory.getLogger(ZipUtil.class);/** * 将字符串压缩后Base64 * @param primStr 待加压加密函数 * @return */public static String zipString(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(Constants.transfer_charset)); 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 unzipString(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(Constants.transfer_charset);} 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;}}使用ZIP进行处理是对于JAVA本身可以互相处理,但是Java压缩的结果C#不能解压,后来修改为GZIP就可以了。
2、GZIP压缩与解压
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import com.fusion.areain.common.Constants;/** * * 对字符串进行加解密和加解压 * @author wujh * */@SuppressWarnings("restriction")public class GZipUtil {private static Logger log = LoggerFactory.getLogger(GZipUtil.class);/** * 将字符串压缩后Base64 * @param primStr 待加压加密函数 * @return */public static String gzipString(String primStr) {if (primStr == null || primStr.length() == 0) {return primStr;}ByteArrayOutputStream out = null; GZIPOutputStream gout = null; try{ out = new ByteArrayOutputStream(); gout = new GZIPOutputStream(out);gout.write(primStr.getBytes(Constants.transfer_charset)); gout.flush();} catch (IOException e) {log.error("对字符串进行加压加密操作失败:", e);return null;} finally {if (gout != null) {try {gout.close();} catch (IOException e) {log.error("对字符串进行加压加密操作,关闭gzip操作流失败:", e);}}}return new BASE64Encoder().encode(out.toByteArray());}/** * 将压缩并Base64后的字符串进行解密解压 * @param compressedStr 待解密解压字符串 * @return */public static final String ungzipString(String compressedStr) {if (compressedStr == null) {return null;}ByteArrayOutputStream out = null;ByteArrayInputStream in = null;GZIPInputStream gin = null;String decompressed = null;try {byte[] compressed = new BASE64Decoder().decodeBuffer(compressedStr);out = new ByteArrayOutputStream();in = new ByteArrayInputStream(compressed);gin = new GZIPInputStream(in);byte[] buffer = new byte[1024];int offset = -1;while((offset = gin.read(buffer)) != -1) {out.write(buffer, 0, offset);}decompressed = out.toString(Constants.transfer_charset);} catch (IOException e) {log.error("对字符串进行解密解压操作失败:", e);decompressed = null;} finally {if (gin != null) {try {gin.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;}public static void main(String[] args) {System.out.println(ungzipString(gzipString("1234567")));}}使用GZIP压缩的时候需要注意,
<strong style="background-color: rgb(204, 102, 0);">return new BASE64Encoder().encode(out.toByteArray());需要放在out对象关闭之后,不然解压时会报错。</strong>
1 0
- Java中ZIP与Gzip的压缩与解压处理,其中有Base64处理
- java中zip与gzip实现文件压缩,解压
- java 处理zip 压缩与解压的问题
- Java实现文件压缩与解压[zip格式,gzip格式]
- Java实现文件压缩与解压[zip格式,gzip格式]
- Java实现文件压缩与解压[zip格式,gzip格式]
- Java实现文件压缩与解压[zip格式,gzip格式]
- java中Gzip压缩与解压技术
- java zip文件的压缩与解压
- java zip 压缩与解压
- zip 的 压缩与解压
- java 处理zip压缩/解压 文件/目录
- web中gzip,deflate的压缩与解压
- ZIP压缩与解压
- Java中ZIP压缩与解压--中文文件名乱码解决办法
- JAVA API实现ZIP压缩与解压
- GZIP/ZIP压缩与解压缩
- JAVA实现实用的ZIP压缩与解压
- IntelliJ IDEA 控制台中文乱码
- java 代理模式
- sprintf、strcpy 及 memcpy 函数
- Web请求和响应---文件上传
- Servlet与一般Java类的区别是什么?
- Java中ZIP与Gzip的压缩与解压处理,其中有Base64处理
- JAVA操作Excel文件 JXL
- Java语音SDK接口开发经验及具体开发实现
- 判断程序是否在后台运行
- UITextView 实现placeholder的方法
- 《Neural Networks for Machine Learning》 by Hinton 学习笔记(一)
- 微信支付安卓集成
- 让返回键失去作用,在新的版本中只需重写方法,并将里面代码注释
- *SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)