GZIP

第一种:返回字节数组
	// 压缩
public static byte[] compress(String str) throws IOException {
Log.i(TAG,"--- compress() start ---");

if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes("UTF-8"));
gzip.close();

Log.i(TAG,"--- compress() end ---");
return out.toByteArray();
}

// 解压缩
public static String decompress(InputStream is) throws IOException {
Log.i(TAG,"--- decompress() start ---");

ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPInputStream gunzip = new GZIPInputStream(is);
byte[] buffer = new byte[BUFFER_SIZE];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}

Log.i(TAG,"--- decompress() end ---");
return out.toString("UTF-8");
}

//解压缩的出来的文件如果是文本文件的话,还可以一行一行的处理
public static void decompress(InputStream is)
throws Exception {
GZIPInputStream gis = new GZIPInputStream(is);
BufferedReader reader = new BufferedReader(new InputStreamReader(gis,"GBK"));
try {
String line;
while((line = reader.readLine()) != null) {
//处理每行数据
}
} finally {
reader.close();
gis.close();
}
}



第二种:返回字符串
1、使用ISO-8859-1作为中介编码,可以保证准确还原数据
2、字符编码确定时,可以在uncompress方法最后一句中显式指定编码

public static String compress(String str) {
byte[] output = new byte[10000];
Deflater compresser = new Deflater();
compresser.setInput(str.getBytes());
compresser.finish();

int compressDataLength = compresser.deflate(output);
String s = null;
try {
s = new String(output, 0, compressDataLength, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}

public static String decompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(str
.getBytes("ISO-8859-1"));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平台默认编码,也可以显式的指定如toString("GBK")
return out.toString();
}


三、如下是从一个网站上偷来的 :oops:
package org.zlex.commons.io;
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;
/**
* GZIP工具
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @since 1.0
*/
public abstract 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 {
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 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 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 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 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();
}
/**
* 文件解压缩
*
* @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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值