网站优化——使用 gzip 压缩文件

本贴最后更新于 2092 天前,其中的信息可能已经水流花落

简介

    Gzip 最早由 Jean-loup Gailly 和 Mark Adler 创建,用于 UNⅨ 系统的文件压缩。我们在 Linux 中经常会用到后缀为.gz 的文件,它们就是 GZIP 格式的。现今已经成为 Internet 上使用非常普遍的一种数据压缩格式,或者说一种文件格式。

编码实现 Gzip

压缩

    public static byte[] gzip(byte[] data) throws Exception {
  	byte[] ret = null;
  	try ( ByteArrayOutputStream bos = new ByteArrayOutputStream();
  			GZIPOutputStream gzip = new GZIPOutputStream(bos);) {
  		gzip.write(data);
  		gzip.finish();
  		gzip.close();
  		ret = bos.toByteArray();
  		bos.close();
  	} catch (Exception e) {
  		e.printStackTrace();
  	}
  	return ret;
  }

## 解压

>```
> 	public static byte[] ungzip(byte[] data) throws Exception {
> 		byte[] ret = null;
> 		try ( ByteArrayInputStream bis = new ByteArrayInputStream(data);
> 				GZIPInputStream gzip = new GZIPInputStream(bis);) {
> 			
> 			byte[] buf = new byte[1024];
> 			int num = -1;
> 			ByteArrayOutputStream bos = new ByteArrayOutputStream();
> 			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
> 				bos.write(buf, 0, num);
> 			}
> 			gzip.close();
> 			bis.close();
> 			ret = bos.toByteArray();
> 			bos.flush();
> 			bos.close();
> 		} catch (Exception e) {
> 			e.printStackTrace();
>		}
>		return ret;
>	}
>```
>
## 测试

> ```
> 	@Test
> 	public void test1() throws Exception {
> 		
> 		String path = "C:/Users/Admin/Desktop/catalina.out"; //选择任意文件进行压缩
> 		InputStream in = new FileInputStream(new File(path));
> 		byte[] bt = new byte[in.available()];
> 		in.read(bt);
> 		in.close();
> 		System.out.println("文件原始大小:\t" + Math.ceil(bt.length / 1024) + "kb");
> 		
> 		byte[] ziped = gzip(bt);
> 		System.out.println("压缩后大小: \t" + Math.ceil(ziped.length / 1024.0) + "kb");
> 		
> 		byte[] unziped = ungzip(ziped);
> 		System.out.println("解压后大小: \t" + Math.ceil(unziped.length / 1024) + "kb");
> 	}
> 
> ```

# 配置`tomcat`开启`Gzip`
>
## 修改`%TOMCAT_HOME%/conf/server.xml`
>
> ```
> <Connector port="8080" protocol="HTTP/1.1"   
>            connectionTimeout="20000"   
>            redirectPort="8443" executor="tomcatThreadPool" URIEncoding="utf-8"   
>                        compression="on"   
>                        compressionMinSize="50" noCompressionUserAgents="gozilla, traviata"   
>                        compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" /> 
> ```
*  `compression="on"` 打开压缩功能 
&nbsp;
*   `compressionMinSize="50"` 启用压缩的输出内容大小,默认为2KB 
&nbsp;
*   `noCompressionUserAgents="gozilla, traviata"` 对于以下的浏览器,不启用压缩 
&nbsp;
*   `compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"` 哪些资源类型需要压缩


# 配置`Nginx`开启`Gzip`
>
## 修改`%NGINX_HOME%/conf/nginx.conf`
找到如下文本:
&nbsp;
![imagepng](http://blog.lesswork.cn/solo/upload/aee47a6f9ccd4d55b0daf9297e8f7660_image.png) 
&nbsp;
&nbsp;
>
*   `gzip off` 开启gzip
&nbsp;
*    `gzip_min_length 1k` 启用gzip压缩的最小阀值 
&nbsp;
*    `gzip_comp_level 1`  gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
&nbsp;
* `gzip_types  xxx/xxx` mime.types 参考[W3cSchoolMIME文档](http://www.w3school.com.cn/media/media_mimeref.asp)
&nbsp;
* `gzip_vary on` CDN和代理服务器使用,针对相同url,可以根据头信息返回压缩和非压缩副本
&nbsp;
* `gzip_disable "MSIE [1-6]\."` 禁用IE 6 gzip
&nbsp;
* `gzip_buffers 32  4k` 设置压缩所需要的缓冲区大小
&nbsp;
* `gzip_http_version 1.1` 设置gzip压缩针对的HTTP协议版本


<br>
***

![](http://blog.lesswork.cn/solo/images/logo.png) 
YouY Blog —— 专心做你的烂笔头。[访问主页](http://blog.lesswork.cn/)

  • Gzip

    gzip (GNU zip)是 GNU 自由软件的文件压缩程序。我们在 Linux 中经常会用到后缀为 .gz 的文件,它们就是 Gzip 格式的。现今已经成为互联网上使用非常普遍的一种数据压缩格式,或者说一种文件格式。

    9 引用 • 12 回帖 • 111 关注
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3167 引用 • 8207 回帖 • 2 关注
  • 并发
    75 引用 • 73 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...