本来是写到spaces live上的,可是代码的显示效果确实不怎么好看。在javaeye上试了试代码显示的顺眼多了。
今天写了个用java压缩的功能,可以实现对文件和目录的压缩。
由于java.util.zip.ZipOutputStream有中文乱码问题,所以采用org.apache.tools.zip.ZipOutputStream。
以下是代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package net.szh.zip;
import <span id="0_nwp" style="width: auto; height: auto; float: none;"><a id="0_nwl" style="text-decoration: none;" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=c58832b3f3d5b78&k=java&k0=java&kdi0=0&luki=3&n=10&p=baidu&q=16031029_cpr&rb=0&rs=1&seller_id=1&sid=785b3d3f2b83580c&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1890924&u=http%3A%2F%2Fwenda%2Essffx%2Ecom%2Fq%2D982%2Ehtml&urlid=0" target="_blank" mpid="0"><span style="width: auto; height: auto; color: rgb(0, 0, 255); font-size: 14px; float: none;">java</span></a></span>.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipCompressor {
static final int BUFFER = 8192;
private File zipFile;
public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void compress(File file, ZipOutputStream out, String basedir) {
/* 判断是目录还是文件 */
if (file.isDirectory()) {
System.out.println("压缩:" + basedir + file.getName());
this.compressDirectory(file, out, basedir);
} else {
System.out.println("压缩:" + basedir + file.getName());
this.compressFile(file, out, basedir);
}
}
/** 压缩一个目录 */
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 递归 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}
/** 压缩一个文件 */
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte <span id="1_nwp" style="width: auto; height: auto; float: none;"><a id="1_nwl" style="text-decoration: none;" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=c58832b3f3d5b78&k=data&k0=data&kdi0=0&luki=2&n=10&p=baidu&q=16031029_cpr&rb=0&rs=1&seller_id=1&sid=785b3d3f2b83580c&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1890924&u=http%3A%2F%2Fwenda%2Essffx%2Ecom%2Fq%2D982%2Ehtml&urlid=0" target="_blank" mpid="1"><span style="width: auto; height: auto; color: rgb(0, 0, 255); font-size: 14px; float: none;">data</span></a></span>[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@佳酿网:
一.摘要
本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度.
二.前言
本文的知识点是从互联网收集整理, 主要来源于中文wiki. 使用YSlow检测网站启用了哪些优化时, Gzip是十分关键的一项. 启动Gip压缩将立竿见影的减少页面的网络传输大小.
三.HTTP压缩概述
HTTP 压缩是在Web服务器和浏览器间传输压缩文本内容的方法。HTTP压缩采用通用的压缩算法如gzip等压缩HTML、Javascript或CSS文件。
压缩的最大好处就是降低了网络传输的数据量,从而提高客户端浏览器的访问速度。
当然,同时也会增加一点点服务器的负担。Gzip是比较常见的一种HTTP 压缩算法。
四.HTTP压缩工作原理
Web服务器处理HTTP压缩的工作原理如下:
1.Web
服务器接收到浏览器的HTTP请求后,检查浏览器是否支持HTTP压缩;
在用户浏览器发送请求的HTTP头中, 带有"Accept-Encoding: gzip, deflate"参数则表明支持gzip和deflate两种压缩算法.
2.如果浏览器支持HTTP压缩,Web服务器检查请求文件的后缀名;静态文件和动态文件后缀启动要所都需要在MetaBase.xml中设置.
静态文件需要设置:
HcFileExtensions Metabase Property
(单击跳转到MSDN说明) 动态文件需要设置: HcscriptFileExtensions Metabase Property (单击跳转到MSDN说明)
3.如果请求文件是HTML、CSS等静态文件并且文件后缀启用了压缩,则Web服务器到压缩缓冲目录中检查是否已经存在请求文件的最新压缩文件;
4.如果请求文件的压缩文件不存在,Web服务器向浏览器返回未压缩的请求文件,并在压缩缓冲目录中存放请求文件的压缩文件;
5.如果请求文件的最新压缩文件已经存在,则直接返回请求文件的压缩文件;
6.如果请求文件是ASPX等动态文件并且文件后缀启用了压缩,Web服务器动态压缩内容并返回浏览器,压缩内容不存放到压缩缓存目录中。
五. 在IIS中启用HTTP压缩
IIS默认并不支持HTTP压缩,需要进行简单的配置
1.打开Internet信息服务(IIS)管理器,右击"网站"->"属性"选择"服务"。在"HTTP压缩"框中选中"压缩应用程序文件"和"压缩静态文件",按需要设置"临时目录"和"临时目录的最大限制";
2.提醒: 经试验此步骤在本人机器上没有作用, 可以忽略. 在 Internet信息服务(IIS)管理器,右击"Web服务扩展"->"增加一个新的Web服务扩展...",在"新建Web服务扩展"框中输入扩名"HTTP Compression",添加"要求的文件"为C:WINDOWSsystem32inetsrvgzip.dll,其中Windows系统目录根据您的安装可能有所不同,选中"设置扩展状态为允许";
3.使用文本编辑器打开C:WindowsSystem32inetsrvMetaBase.xml(建议先备份), 找到Location ="/LM/W3SVC/Filters/Compression/gzip用于设置gzip压缩,找到Location ="/LM/W3SVC/Filters/Compression/deflate"用于设置deflate压缩. 上面两个节点紧挨着.并且设置的属性相同.
如果需要压缩动态文件,则将HcDoDynamicCompression设置为"TRUE",并在HcscriptFileExtensions中增加您要压缩的动态文件后缀名,如aspx;如果需要压缩静态文件,则将HcDoStaticCompression和 HcDoOnDemandCompression设置为"TRUE",并在HcFileExtensions中增加您需要压缩的静态文件后缀名,如 xml、css等;HcDynamicCompressionLevel和HcOnDemandCompLevel表示需要的压缩率,数值在0-10, 默认为0. HcDynamicCompressionLevel属性说明:
HcDynamicCompressionLevel Metabase PropertyHcOnDemandCompLevel 属性说明:HcOnDemandCompLevel Metabase Property说明: 这两个属性值一般推荐设置为
9, 具有最佳性价比.但是在我的window server 2003上, 压缩率无论如何设置, jQuery和jQuery
UI两个文件(58k/188k)压缩后的大小一直相同.(20k/45k). 下面是我的实例: Xml代码
1.<IIsCompressionSchemeLocation="/LM/W3SVC/Filters/Compression/deflate"
2.HcCompressionDll="%windir%system32inetsrvgzip.dll"
3.HcCreateFlags="0"
4.HcDoDynamicCompression="TRUE"
5.HcDoOnDemandCompression="TRUE"
6.HcDoStaticCompression="true"
7.HcDynamicCompressionLevel="9"
8.HcFileExtensions="htm
9. html
10. txt
11. js
12. css
13. swf
14. xml"
15.
HcOnDemandCompLevel="9"
16. HcPriority="1"
17. HcscriptFileExtensions="asp
18. aspx
19. dll
20. exe"
21. >
22.</IIsCompressionScheme>
23.<IIsCompressionSchemeLocation="/LM/W3SVC/Filters/Compression/gzip"
24. HcCompressionDll="%windir%system32inetsrvgzip.dll"
25. HcCreateFlags="1"
26. HcDoDynamicCompression="TRUE"
27. HcDoOnDemandCompression="TRUE"
28. HcDoStaticCompression="true"
29. HcDynamicCompressionLevel="9"
30. HcFileExtensions="htm
31. html
32. txt
33. js
34. css
35. swf
36. xml"
37. HcOnDemandCompLevel="9"
38. HcPriority="1"
39.
HcscriptFileExtensions
="asp
40. aspx
41. dll
42. exe"
43. >
44.</IIsCompressionScheme>
.csharpcode, .csharpcode pre { font-size: small; color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre
{ margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd
{
color:
#0000ff;
}
.csharpcode
.str
{
color:
#006080;
}
.csharpcode
.op
{ color: #0000c0; } .csharpcode .preproc { color:
#cc6633; } .csharpcode .asp { background-color:
#ffff00;
}
.csharpcode
.html
{
color:
#800000;
}
.csharpcode
.attr
{
color:
#ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%;
margin: 0em; } .csharpcode .lnum { color: #606060; }
4.编辑完毕后保存MetaBase.xml文件;如果文件无法保存,则可能IIS正在使用该文件。打开"开始"->"管理工具"->"服务",停止"IIS Admin Service"后,即可保存
5.最后,重新启动IIS。可以到HTTP压缩测试网站验证结果.以jQuery为例, 核心类库和UI类库原始大小分别是57k和188k,压缩后分别是20k和45k:
我们通过Http头中的: Content-Encoding:gzip 属性判断返回后的数据已经启用了gzip压缩:
使用YSlow检测, 当只启动静态文件压缩时:
Gzip压缩评级为B:
当同时启动了动态文件压缩时, Gzip压缩评级为A:
@廖维林:
@万可佳:
CSS压缩:http://www.ssffx.com/css/
html还是不要压缩了,你压缩以后基本没人可以看懂了,建议做css压缩就可以了。
不过还可以做zip压缩,看你的空间是否支持。