天天看點

不錯的開源壓縮元件(可惜沒能用上)

今天繼續在研究Silverlight的釋出優化方法,由于項目需要開啟OOB,沒有辦法再選中 

<a target="_blank" href="http://blog.51cto.com/attachment/201106/222549931.png"></a>

這兩個選項隻能選擇其中一個,很遺憾不能利用自身的功能合并項目了,這裡說明一下,第一個選項的好處

如果你的解決方案中有兩個Silverlight項目,那麼如果選擇了第一項,則兩個項目中共同的DLL會被壓縮放在ClientBin目錄下,這樣做的好處是避免了兩個SL項目中引用重複的DLL,但是選擇OOB之後,我們便不能選擇這個項了,于是我想到在OOB的基礎上,将打包好的XAP改成壓縮檔案,再解壓,将兩個項目中的公共DLL抽取出來放到ClientBin下,當然這樣做還涉及到Xap包中的一個檔案 

代碼

&lt;Deployment xmlns="&lt;a href="http://schemas.microsoft.com/client/2007/deployment""&gt;http://schemas.microsoft.com/client/2007/deployment"&lt;/a&gt; 

xmlns:x="&lt;a href="http://schemas.microsoft.com/winfx/2006/xaml""&gt;http://schemas.microsoft.com/winfx/2006/xaml"&lt;/a&gt; EntryPointAssembly="ProjectA" 

EntryPointType="ProjectA.App" RuntimeVersion="4.0.50401.0"&gt; 

  &lt;Deployment.OutOfBrowserSettings&gt; 

    &lt;OutOfBrowserSettings ShortName="ProjectA Application" EnableGPUAcceleration="False" 

ShowInstallMenuItem="True"&gt; 

      &lt;OutOfBrowserSettings.Blurb&gt;ProjectA Application on your desktop; at home, at work or on the 

go.&lt;/OutOfBrowserSettings.Blurb&gt; 

      &lt;OutOfBrowserSettings.WindowSettings&gt; 

        &lt;WindowSettings Title="ProjectA Application" /&gt; 

      &lt;/OutOfBrowserSettings.WindowSettings&gt; 

      &lt;OutOfBrowserSettings.Icons /&gt; 

    &lt;/OutOfBrowserSettings&gt; 

  &lt;/Deployment.OutOfBrowserSettings&gt; 

  &lt;Deployment.Parts&gt; 

    &lt;AssemblyPart x:Name="ProjectA" Source="ProjectA.dll" /&gt; 

    &lt;AssemblyPart x:Name="ProjectB" Source="ProjectB.dll" /&gt; 

    &lt;AssemblyPart x:Name="System.Windows.Controls" Source="System.Windows.Controls.dll" /&gt; 

    &lt;AssemblyPart x:Name="Telerik.Windows.Controls" Source="Telerik.Windows.Controls.dll" /&gt; 

  &lt;/Deployment.Parts&gt; 

&lt;/Deployment&gt;

 我想将最後兩個DLL抽取出來或者改變一下配置路徑,但兩種方法都沒有用,有了解的朋友還請指點。

在解決問題的過程中我下載下傳了一個挺不錯的壓縮元件

SharpZipLib 有興趣的朋友可以試試

<a href="http://www.icsharpcode.net/OpenSource/SharpZipLib/">http://www.icsharpcode.net/OpenSource/SharpZipLib/</a>

簡單寫了一個類實作壓縮和解壓縮功能。

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web;

using System.IO; 

using ICSharpCode.SharpZipLib.Zip;

namespace SLProjectsDownLoad.Web 

    public class Zip 

    { 

        public static void ZipFiles(string ZipFileName, string FromDirectory) 

        { 

            FastZip fz = new FastZip(); 

            fz.CreateEmptyDirectories = true; 

            fz.CreateZip(ZipFileName, FromDirectory, true, ""); 

            fz = null; 

        }

        public static void UnZipFiles(string ZipFileName, string ToDirectory) 

            if (!Directory.Exists(ToDirectory)) 

                Directory.CreateDirectory(ToDirectory);

            ZipInputStream s = new ZipInputStream(File.OpenRead(ZipFileName));

            ZipEntry theEntry; 

            while ((theEntry = s.GetNextEntry()) != null) 

            { 

                string directoryName = Path.GetDirectoryName(theEntry.Name); 

                string fileName = Path.GetFileName(theEntry.Name);

                if (directoryName != String.Empty) 

                    Directory.CreateDirectory(ToDirectory + directoryName);

                if (fileName != String.Empty) 

                { 

                    FileStream streamWriter = File.Create(ToDirectory + theEntry.Name);

                    int size = 2048; 

                    byte[] data = new byte[2048]; 

                    while (true) 

                    { 

                        size = s.Read(data, 0, data.Length); 

                        if (size &gt; 0) 

                        { 

                            streamWriter.Write(data, 0, size); 

                        } 

                        else 

                            break; 

                    } 

                    streamWriter.Close(); 

                } 

            } 

            s.Close(); 

        } 

    } 

}

     本文轉自wengyuli 51CTO部落格,原文連結:http://blog.51cto.com/wengyuli/586793,如需轉載請自行聯系原作者

繼續閱讀