gzip压缩笔记
发布日期:2021-06-30 18:39:47 浏览次数:3 分类:技术文章

本文共 6097 字,大约阅读时间需要 20 分钟。

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:

转载地址:https://linqiarui.blog.csdn.net/article/details/51296859 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:常见的content-type
下一篇:内存缓存与LruCache

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月16日 18时13分03秒