android zip怎么压缩,android自带zip轻松实现压缩解压

android自带zip轻松实现压缩解压

开发过程用到了zip压缩包,写了一个工具类,该类可以实现把字符串直接压缩成zip格式,省去了写入文件再压缩的步骤:

/**

*

* @author shx

* 压缩和解压缩工具

*

*/

public class ZipUtil {

/**

* 压缩方法

* @param str 要压缩的字符串

* @param path路径

* @throws IOException

*/

public static void compress(String str,String path) throws IOException {

if (null == str || str.length() <= 0) {

return;

}

FileOutputStream fileOutputStream = new FileOutputStream(path);

GZIPOutputStream gzip = new GZIPOutputStream(fileOutputStream);

gzip.write(str.getBytes("utf-8"));

gzip.close( );

fileOutputStream.close();

}

/**

* 解压缩

* @param context

* @param path

* @return

*/

public static String unCompress(Context context,String path) {

try {

File file = new File(path);

if (!file.exists()) {

return context.getResources().getString(R.string.FileNotExits);

}

ByteArrayOutputStream out = new ByteArrayOutputStream();

// 创建一个新的输出流

FileInputStream fileInputStream = new FileInputStream(path);

GZIPInputStream gzip = new GZIPInputStream(fileInputStream);

byte[] buffer = new byte[256];

int n = 0;

// 将未压缩数据读入字节数组

while ((n = gzip.read(buffer)) >= 0) {

out.write(buffer, 0, n);

}

return out.toString("utf-8");

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

http://www.dengb.com/Androidjc/872012.htmlwww.dengb.comtruehttp://www.dengb.com/Androidjc/872012.htmlTechArticleandroid自带zip轻松实现压缩解压 开发过程用到了zip压缩包,写了一个工具类,该类可以实现把字符串直接压缩成zip式,省去了写入文件再压...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值