您的位置:首页 > 其它

gzip压缩笔记

2016-05-02 16:23 155 查看

gzip

一种压缩格式,一种压缩方式,可以对
网络传输的数据
进行压缩.减少网络传输的大小

为什么需要压缩?

减少体积,提高传输速度,提高用户体验

浏览器发送器请求的过程?

1.发送请求头:
Accept-Encoding:gzip


2.服务器压缩数据,返回数据,在响应头里面添加
Content-Encoding:gzip


3.客户端,根据Content-Encoding这个响应头,对应解压

有Content-Encoding:gzip–>gzip解压

没有Content-Encoding:gzip–>标准解压

app使用gzip压缩

返回的json/xml(文本信息)其实就是个特殊的网页,其实也是可以进行gzip压缩

gzip压缩效果

通过数据,我们得知,文本的压缩率,大概可以达到70%左右.压缩率很高;

gzip压缩的实现

try {
boolean isGzip = false;
//1.创建httpclient
DefaultHttpClient httpClient = new DefaultHttpClient();
//2.创建get请求
HttpGet get = new HttpGet("http://httpbin.org/gzip");
//① 添加请求头 Accept-Encoding:"gzip, deflate"
get.addHeader("Accept-Encoding", "gzip");
//3.执行请求
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
//② 得到响应头,Content-Encoding:"gzip"
Header[] headers = response.getHeaders("Content-Encoding");
for (Header header : headers) {
if (header.getValue().equals("gzip")) {//后台server把数据进行了gzip压缩
isGzip = true;
}
}
String result = "";
HttpEntity entity = response.getEntity();
//③根据是否使用gzip压缩.采取不同的解压方式
if (isGzip) {
//④进行gzip的解压
GZIPInputStream in = new GZIPInputStream(response.getEntity().getContent());
//in-->string
result = convertStreamToString(in);
} else {
//4.打印结果
result = EntityUtils.toString(entity);
}
System.out.println("result:" + result);
}
} catch (Exception e) {
e.printStackTrace();
}


测试请求的地址

http://httpbin.org


安卓中对gzip的请求与处理完整代码:

MainActivity:

package com.lqr.zipdemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

/**
* @author CSDN_LQR
* @工程 7ZipDemo
* @包名 com.lqr.zipdemo
* @TODO 请求一个支持gzip的网址后,进行gzip解压
*/
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

init7Zip();
}

private void init7Zip() {
findViewById(R.id.btnGzip).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
new Thread() {
public void run() {

// 是否使用了gzip压缩
boolean isGzip = false;
try {
// 初始化httpClient对象
DefaultHttpClient client = new DefaultHttpClient();

// 初始化httpGet对象(这是一个支持gzip压缩的网址)
HttpGet get = new HttpGet("http://httpbin.org/gzip");

// 1.发送请求头:Accept-Encoding:gzip
get.addHeader("Accept-Encoding", "gzip");

// 发起请求
HttpResponse response = client.execute(get);

if (response.getStatusLine().getStatusCode() == 200) {

// 2.取的响应头`Content-Encoding`,判断是否包含Content-Encoding:gzip
Header[] headers = response
.getHeaders("Content-Encoding");
for (Header header : headers) {
if (header.getValue().equals("gzip")) {
isGzip = true;
}
}

HttpEntity entity = response.getEntity();
String result = "";

// 3.相应的gzip解压,不解压会乱码
if (isGzip) {
InputStream is = entity.getContent();
GZIPInputStream gis = new GZIPInputStream(
is);
// inputStream-->string
result = convertStreamToString(gis);
} else {// 标准解压
result = EntityUtils.toString(entity);
}

System.out.println(result);
}

} catch (Exception e) {
// TODO
e.printStackTrace();
}
};
}.start();
}
});
}

/**
* 将输入流转成String
*
* @param is
* @return
* @throws IOException
*/
public static String convertStreamToString(InputStream is)
throws IOException {
try {
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "utf-8"));
// BufferedReader reader = new BufferedReader(new
// InputStreamReader(is));
while ((line = reader.readLine()) != null) {
// sb.append(line);
sb.append(line).append("\n");
}
} finally {
is.close();
}
return sb.toString();
} else {
return "";
}
} catch (Exception e) {
e.printStackTrace();
return "";
}

}
}


activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/btnGzip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7zip" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: