在NAnt中可以使用<zip>標籤進行檔案壓縮打包,如下所示:
<target name="backup">
<zip zipfile="${backup.dir}\Src_Bak.zip">
<fileset basedir=".">
<includes name="**" />
<excludes name="**\bin\**" />
<excludes name="**\obj\**" />
</fileset>
</zip>
</target>
原以為很簡單的事,在實際使用中遇到了小麻煩,含有中文路徑的檔案會出錯。癥狀是可以正常打包,但在解壓時會提示該檔案異常,無法解壓。
通過查NAnt的線上協助(http://nant.sourceforge.net/help),發現NAnt中的zip壓縮是調用SharpZipLib(註:用C#寫的開源的壓縮函數庫,網址http://www.icsharpcode.net/OpenSource/SharpZipLib/),最新的SharpZipLib的版本是0.81.0.1407版。
然而NAnt 0.84版中採用的是SharpZipLib 0.5.0.0版的!!估計問題可能就出在這兒。從SourceForge下載源檔案http://optusnet.dl.sourceforge.net/sourceforge/nant/nant-0.84.zip並解壓,從081SharpZipLib.zip處下載SharpZipLib 0.81.0.1407版,將ICSharpCode.SharpZipLib.dll替換掉NAnt中lib目錄下的同名檔案。
開啟\src\NAnt.Zip\Tasks\ZipTask.cs檔案,注釋掉164行代碼,如下所示:
160 // set compression level
161 if (ZipLevel > 0) {
162 zOutstream.SetLevel(ZipLevel);
163 } else {
164 //zOutstream.SetMethod(ZipOutputStream.STORED); //注釋掉這一行,因為新版的SharpZipLib中沒有這個方法
166 // setting the method to store still compresses the files
167 // setting the level to 0 fixes this
168 zOutstream.SetLevel(ZipLevel);
169 }
重新編譯NAnt,用編譯產生的NAnt.ZipTasks.dll以及下載的最新SharpZipLib庫檔案覆蓋舊的NAnt同名檔案即可