gzip工具类 支持字符串压缩解压缩
来源:互联网 发布:ueditor java图片上传 编辑:程序博客网 时间:2024/05/16 10:26
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * GZIP工具 * */ public abstract class GZipUtils { public static final int BUFFER = 1024; public static final String EXT = ".gz"; private static String encode = "utf-8";//"ISO-8859-1" private static String hexStr ="0123456789ABCDEF";//全局 /** * 数据压缩 * * @param data * @return * @throws IOException * @throws Exception */ public static byte[] compress(byte[] data) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 压缩 compress(bais, baos); byte[] output = baos.toByteArray(); baos.flush(); baos.close(); bais.close(); return output; } /** * 字符串解压缩 * * @param data * @return * @throws IOException * @throws Exception */ public static String compressStr(String data) throws IOException { return BinaryToHexString(compress(data.getBytes(encode))); } /** * 文件压缩 * * @param file * @throws Exception */ public static void compress(File file) throws Exception { compress(file, true); } /** * 文件压缩 * * @param file * @param delete * 是否删除原始文件 * @throws Exception */ public static void compress(File file, boolean delete) throws Exception { FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getPath() + EXT); compress(fis, fos); fis.close(); fos.flush(); fos.close(); if (delete) { file.delete(); } } /** * 数据压缩 * * @param is * @param os * @throws IOException * @throws Exception */ public static void compress(InputStream is, OutputStream os) throws IOException { GZIPOutputStream gos = new GZIPOutputStream(os); int count; byte data[] = new byte[BUFFER]; while ((count = is.read(data, 0, BUFFER)) != -1) { gos.write(data, 0, count); } gos.finish(); gos.flush(); gos.close(); } /** * 文件压缩 * * @param path * @throws Exception */ public static void compress(String path) throws Exception { compress(path, true); } /** * 文件压缩 * * @param path * @param delete * 是否删除原始文件 * @throws Exception */ public static void compress(String path, boolean delete) throws Exception { File file = new File(path); compress(file, delete); } /** * 字符串解压缩 * * @param data * @return * @throws IOException * @throws Exception */ public static String decompressStr(String data) throws IOException { return new String(decompress(HexStringToBinary(data)),encode); } /** * 数据解压缩 * * @param data * @return * @throws IOException * @throws Exception */ public static byte[] decompress(byte[] data) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 解压缩 decompress(bais, baos); data = baos.toByteArray(); baos.flush(); baos.close(); bais.close(); return data; } /** * 文件解压缩 * * @param file * @throws Exception */ public static void decompress(File file) throws Exception { decompress(file, true); } /** * 文件解压缩 * * @param file * @param delete * 是否删除原始文件 * @throws Exception */ public static void decompress(File file, boolean delete) throws Exception { FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT, "")); decompress(fis, fos); fis.close(); fos.flush(); fos.close(); if (delete) { file.delete(); } } /** * 数据解压缩 * * @param is * @param os * @throws IOException * @throws Exception */ public static void decompress(InputStream is, OutputStream os) throws IOException { GZIPInputStream gis = new GZIPInputStream(is); int count; byte data[] = new byte[BUFFER]; while ((count = gis.read(data, 0, BUFFER)) != -1) { os.write(data, 0, count); } gis.close(); } /** * 文件解压缩 * * @param path * @throws Exception */ public static void decompress(String path) throws Exception { decompress(path, true); } /** * 文件解压缩 * * @param path * @param delete * 是否删除原始文件 * @throws Exception */ public static void decompress(String path, boolean delete) throws Exception { File file = new File(path); decompress(file, delete); } public static String BinaryToHexString(byte[] bytes){ String result = ""; String hex = ""; for(int i=0;i<bytes.length;i++){ //字节高4位 hex = String.valueOf(hexStr.charAt((bytes[i]&0xF0)>>4)); //字节低4位 hex += String.valueOf(hexStr.charAt(bytes[i]&0x0F)); result +=hex; } return result; } /** * * @param hexString * @return 将十六进制转换为字节数组 */ public static byte[] HexStringToBinary(String hexString){ //hexString的长度对2取整,作为bytes的长度 int len = hexString.length()/2; byte[] bytes = new byte[len]; byte high = 0;//字节高四位 byte low = 0;//字节低四位 for(int i=0;i<len;i++){ //右移四位得到高位 high = (byte)((hexStr.indexOf(hexString.charAt(2*i)))<<4); low = (byte)hexStr.indexOf(hexString.charAt(2*i+1)); bytes[i] = (byte) (high|low);//高地位做或运算 } return bytes; } }
阅读全文
0 0
- gzip工具类 支持字符串压缩解压缩
- GZip压缩解压缩工具类
- ZLib,GZip,Zip压缩与解压缩工具类
- JAVA字符串的GZIP压缩解压缩方法
- JAVA字符串的GZIP压缩解压缩方法
- JAVA字符串的GZIP压缩解压缩方法
- 基于Gzip的压缩解压缩帮助类
- Gzip压缩解压缩
- php gzip压缩解压缩
- gzip压缩解压缩数据
- HttpURLConnection工具类 支持Gzip
- java 压缩&解压缩 工具类
- Zip压缩、解压缩方法(GZip)
- java Gzip 压缩与解压缩
- GZIP/ZIP压缩与解压缩
- java Gzip 压缩与解压缩
- 【Unix/Linux】【命令】【压缩/打包】压缩/解压缩工具 —— gzip命令
- java实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
- js中click事件被多次执行
- mybatis 自增主键配置
- 如何关闭 Mac 开机时发出的「咚」的声音?
- 剑指offer面试题[8]-旋转数组的最小数字
- Angular2之入门示例
- gzip工具类 支持字符串压缩解压缩
- 看来微服务就是一把双刃剑
- 直方图法、Kn近邻估计法、Parzen窗法
- 2.7更改菜单命令的文本
- 操作系统(二):进程与线程管理_2
- 华为:图片整理
- 2.8更改命令的外观
- UITableViewCell以及其子控件自适应高度
- JAVA有三大特性之--- 多态(个人理解)