android ZLib压缩/解压缩

为了优化接口数据传输速度和节省流量,该项目采用二进制格式并利用ZLib进行压缩。在Android应用端,需要处理二进制数据的解析以及ZLib解压缩操作。以下是实现这一功能的代码片段。
摘要由CSDN通过智能技术生成

项目里面有一个接口要频繁的刷新数据,同时数据量有很大,为了优化传输速度和节省流量,决定使用传输二进制数据,并将数据进行ZLib压缩。这就需要APP端进行二进制数据的解析和对数据进行ZLib解压缩.

不多说了,具体代码如下:

//把文件转换成byte[]数组

	//把文件转换成byte[]数组
	byte[] bytes0 = FileUtils.toByteArray(getExternalFilesDir(null).getPath()
        	+ File.separator+byteImgName);
	//ZLib解压缩数据
	byte[] bytes = ZLibUtils.decompress(bytes0);


  

FileUtils:
	
public class FileUtils{
    /** 
      * the traditional io way  
      * @param filename 
      * @return 
      * @throws IOException 
      */
    public static byte[] toByteArray0(String filename) throws IOException{

        File f = new File(filename);
        if(!f.exists()){
        throw new FileNotFoundException(filename);}

        ByteArrayOutputStream bos = new ByteArrayOutputStream((int)f.length());
        BufferedInputStream in = null;
        try{
            in = new BufferedInputStream(new FileInputStream(f));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while(-1 != (len = in.read(buffer,0,buf_size))){
                bos.write(buffer,0,len);
            }
            return bos.toByteArray();
         }catch (IOException e) {
            e.printStackTrace();
            throw e;
        }finally{
            try{
                in.close();
            }catch(IOException e){
                 e.printStackTrace();
            }
            bos.close();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值