package util;

 import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; public class GZipUtils {
public static final int BUFFER = 1024;
public static final String EXT = ".gz"; /**
* 数据压缩
* @param data
* @return
* @throws Exception
*/
public static byte[] compress(byte[] data) throws Exception{
byte[] output = null;
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 压缩
compress(bais, baos);
output = baos.toByteArray(); baos.flush();
baos.close(); bais.close();
return output;
} /**
* 文件压缩
*
* @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) {
boolean fal=file.delete();
}
} /**
* 数据压缩
*
* @param is
* @param os
* @throws Exception
*/
public static void compress(InputStream is, OutputStream os)
throws Exception { 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 Exception
*/
public static byte[] decompress(byte[] data) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 解压 decompress(bais, baos); data = baos.toByteArray(); baos.flush();
baos.close(); bais.close(); return data;
} /**
* 数据解压
*
* @param is
* @param os
* @throws Exception
*/
public static void decompress(InputStream is, OutputStream os)
throws Exception {
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();
} }
有同事写解压方法的时候直接返回 String 类型,再获取 byte 用 base64 包装,事实说明
ByteArrayOutputStream.toByteArray 输出的结果一般情况下并不能转成 String 类型再转回 byte 类型。
参考:https://zhidao.baidu.com/question/1540471893185651307.html
byte数组转字符串在特定情况下是可以的,byte的内容全部可以用字符表示,否则的话,转换的时候,有些无法用字符表示的数据,在转换的过程中就会出现问题。
如果需要转字符串,可以对byte数组进行base六十四编码,这个base六十四编码的byte可以全部用字符表示,转成字符串,需要的时候,把字符串进行base六十四解密,可以得到原来的byte数组
												

最新文章

  1. SQL Server 数据库设计规范
  2. 其它数据类型和Json的转化
  3. jquery上传文件控件Uploadify
  4. Ubuntu apt命令
  5. Web 在线文件管理器学习笔记与总结(3)创建文件
  6. 下拉刷新控件(3)系统自带的下拉刷新控件SwipeRefreshLayout(推荐*)
  7. Android Handler、Lopper消息驱动机制
  8. TCP编程的一个小例子
  9. perl 访问网站一些useragent的设置
  10. linux_操作系统
  11. python --- queue模块使用
  12. JVM内存越多,能创建的线程越少,越容易发生java.lang.OutOfMemoryError: unable to create new native thread。
  13. Linux LVM学习总结——Insufficient Free Extents for a Logical Volume
  14. 常用的phpdoc标签
  15. [国家集训队]排队 [cdq分治]
  16. websocket 的客户端 websocket-sharp
  17. android 插件合集
  18. SPListItem.UpdateOverwriteVersion()真的不会创建新版本吗?
  19. UITableView+FDTemplateLayoutCell源码学习笔记
  20. PowerDesigner最基础的使用方法入门学习(转载)

热门文章

  1. idea新建maven web项目
  2. Tornado中的Cookie设置
  3. POJ 1003:Hangover
  4. BZOJ 3442 学习小组
  5. SQL left join、rignt join、inner join区别
  6. input自动填入密码以后变成白色和黄色的解决办法
  7. xv6的启动过程
  8. Cracking Digital VLSI Verification Interview 第三章
  9. centos6-7 yum安装php的方法
  10. 两个exe共享内存数据