HTTP gzip压缩( 二 )

okhttp gzip压缩/解压 (示例)//zip压缩GzipSink gzipSink = new GzipSink(Okio.sink(file));BufferedSink bufferedSink = Okio.buffer(gzipSink);bufferedSink.writeUtf8("this is zip file");bufferedSink.flush();bufferedSink.close();//读取zipGzipSource gzipSource = new GzipSource(Okio.source(file));BufferedSource bufferedSource = Okio.buffer(gzipSource);String s = bufferedSource.readUtf8();okhttp框架-如何对请求(request)数据进行GZIP压缩-GzipRequestInterceptor
OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(new GzipRequestInterceptor())//开启Gzip压缩....build();GzipRequestInterceptorhttps://github.com/square/okhttp\issues/350#issuecomment-123105641class GzipRequestInterceptor implements Interceptor {@Override public Response intercept(Chain chain) throws IOException { Request originalRequest = chain.request(); if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {return chain.proceed(originalRequest);}Request compressedRequest = originalRequest.newBuilder() .header("Content-Encoding", "gzip") .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body()))) .build(); return chain.proceed(compressedRequest);}/** https://github.com/square/okhttp\issues/350 */private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {final Buffer buffer = new Buffer();requestBody.writeTo(buffer);return new RequestBody() {@Overridepublic MediaType contentType() {return requestBody.contentType();}@Overridepublic long contentLength() {return buffer.size();}@Overridepublic void writeTo(BufferedSink sink) throws IOException {sink.write(buffer.snapshot());}};}private RequestBody gzip(final RequestBody body) {return new RequestBody() {@Overridepublic MediaType contentType() {return body.contentType();}@Overridepublic long contentLength() {return -1; // We don't know the compressed length in advance!}@Overridepublic void writeTo(BufferedSink sink) throws IOException {BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));body.writeTo(gzipSink);gzipSink.close();}};}}okhttp框架-如何对请求数据进行GZIP压缩
https://cloud.tencent.com/info/61307ab74137a46628c2ea2ca42a6eb4.html
Okhttp3请求网络开启Gzip压缩 - CSDN博客
https://blog.csdn.net/aiynmimi/article/details/77453809
四. Nginx的Gzip可以对服务器端响应内容进行压缩从而减少一定的客户端响应时间gzip on;gzip_min_length 1k;gzip_buffers 4 32k;gzip_types text/plain application/x-JavaScript application/javascript text/xml text/css;gzip_vary on;API网关那些儿 | I'm Yunlong
http://ylzheng.com/2017/03/14/the-things-about-api-gateway
source: //liuxiang.github.io/2018/08/13/HTTP%20gzip压缩



推荐阅读