2009-07-01 114 views
1

我剛剛創建了Javascript &使用YUI Compressor優化文件大小的CSS處理程序& GZip或Deflate壓縮程序取決於請求標頭。以下代碼用於通過GZipStream類壓縮文件。但我發現它只能從減少JQuery 1.3.2(YUI壓縮)文件大小58KB - > 54KB用GZip,Deflate壓縮javascript文件的最佳組件是什麼?

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)?

+0

我認爲它發生偏轉,其DEFLATE – rahul 2009-07-01 07:02:11

+0

對不起。這是我的錯誤。 – 2009-07-01 07:16:08

回答

0

是的,內置壓縮並不是那麼好。嘗試使用Ionic.zip庫,它可以提供更好的壓縮比。

0

嘗試zlib.net(http://www.componentace.com/zlib_.NET.htm)進行壓縮。

我有一個內容長度爲14588的頁面未壓縮。

.NET的原生gzip的帶來下來到2909 zlib.net的gzip的帶來下來到2590

.NET的原生放氣帶來下來到2891 zlib.net的放氣用「最好的帶來下來到2559壓縮「設置和2583默認設置。

它也很簡單:

using zlib; 
    Response.Filter = new ZOutputStream(Response.Filter, zlib.zlibConst.Z_BEST_COMPRESSION); 
    Response.AppendHeader("Content-Encoding", "deflate");