Android Gzip压缩和解压缩文件

   为了节省流量用到了Gzip压缩文件。

 参考此链接中的代码https://gist.github.com/ansjsun/5791394

importjava.io.ByteArrayInputStream;

 
  import java.io.ByteArrayOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.zip.GZIPInputStream;
  import java.util.zip.GZIPOutputStream;
   
  public class StringZip {
  public static void main(String[] args) throws Exception {
  StringBuilder value = new StringBuilder() ;
  for (int i=0; i< 100; i++) {
  value.append("java gzip 压缩测试");
  }
  //字符串压缩为byte数组
  byte[] values = value.toString().getBytes() ;
  System.out.println("解压前大小"+values.length);
  values = compress(values) ;
  System.out.println("解压后大小"+values.length);
  //把压缩后的byte数组转为字符串
  String str = new String(values,"iso8859-1") ;
   
  //传输字符串
  System.out.println(str);
   
  //将接受到的字符串转换为byte数组
  values = str.getBytes("iso8859-1") ;
  //解压缩这个byte数组
  values = decompress(values) ;
  System.out.println(newString(values,"utf-8"));
   
  }
   
  /**
  * 数据解压缩
  *
  * @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();
  }
   
  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;
  }
  static final int BUFFER = 10240 ;
  /**
  * 数据压缩
  *
  * @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();
  }
   
  }

以下是我的部分代码片段 函数中压缩了一个 xml文件,

public void sendmessage2() {
    try {
       OutputStream out =  socket.getOutputStream();
        StringBuilder xml = new StringBuilder();
        xml.append("\"1.0\" encoding=\"UTF-8\"?>");
        xml.append("\"0\" Cmd=\"1\" Cla=\"17\" Name=\"js6\">");
        xml.append("\"123456\" Para=\"js4\"/>\n");
        xml.append("");

        //字符串压缩为byte数组
        byte[] values ;
        values = compress(xml.toString().getBytes("utf-8")) ;
        //传输字符串
       out.write(values);


        Message m = new Message();
        m.what = 1;
        handler.sendMessage(m);
    } catch (Exception e) {
        e.printStackTrace();
        Message m = new Message();
        m.what = 2;
        handler.sendMessage(m);
    }
}
//解压代码
public void receievmessage(){
    try{
        while(true){
            DataInputStream input = new DataInputStream(socket.getInputStream());
            byte[] buffer;
            //读取input中没有读取的字节
            buffer = new byte[input.available()];
            int len;
            if(buffer.length != 0){
                System.out.println("获取到字节");
                GZIPInputStream gzip =new GZIPInputStream(input);
                        // 读取缓冲区
                len= gzip.read(buffer);
                System.out.println("接收的长度: "+len);
                char[] tChars=new char[300];
                for(int i=0;ichar)buffer[i];  }
                StringBuffer  tStringBuf=new StringBuffer ();
                tStringBuf.append(tChars);

                System.out.println("接收的内容是: "+ tStringBuf);
               // System.out.println("接收的长度: "+buffer);
               // String msg = new String(buffer, "utf-8");//注意转码,不然中文会乱码。
              //  Message m = new Message();
              //  m.what = 3;
              //  m.obj = msg;
                //handler.sendMessage(m);
            }
        }

    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        Message m = new Message();
        m.what = 2;
        handler.sendMessage(m);
    }
}

你可能感兴趣的:(Android Gzip压缩和解压缩文件)