我刚刚创建了用于优化文件大小的 Javascript 和 CSS 处理程序,使用 YUI Compressor 和 GZip 或 Deflate 压缩器取决于请求标头。以下代码用于通过 GZipStream 类压缩文件。但我发现它可以将 JQuery 1.3.2(YUI 压缩)文件大小从58KB -> 54KB减少。
public void GZipCompressFile(string filePath, string compressedFilePath)
{
FileStream sourceFile = File.OpenRead(filePath);
FileStream destFile = File.Create(compressedFilePath);
using (GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress))
{
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
theByte = sourceFile.ReadByte();
}
}
sourceFile.Dispose();
destFile.Dispose();
}
另一方面,我使用 gz 格式(超压缩模式)使用 7-Zip 压缩文件。压缩文件的大小仅为19.3 KB。但是,我不能使用 7-Zip 作为默认压缩器。因为它无法使用 Deflate 压缩器方法压缩文件。
你有任何组件来压缩 GZip 和 Deflate 文件类型的文件吗?或者您对编辑我的 GZipCompressFile 方法有任何想法吗?此外,是否所有新浏览器(IE8、FF3.5、Opera 9.6、Safari 4)都支持压缩方法(GZip 和 Deflate)?