using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;



#region 壓縮檔案
//來源資料夾路徑
string sourceFiles = (System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\Output\\" + YrId + SsId + "\\").Replace("/", "\\");
//目的資料夾路徑
string zipFile = (System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\OutZip\\").Replace("/", "\\") + YrId + SsId + ".zip";
// 傳入參數: 來源路徑, 目的壓縮檔名(.zip), 壓縮比( 0=僅儲存, 9=最高壓縮比 )
Compress(sourceFiles, zipFile, 9);
#endregion
 public static void Compress(string dir, string zipFileName, int level)
    {
        string[] filenames = Directory.GetFiles(dir);
        byte[] buffer = new byte[4096];


        using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))
        {
            // 設定壓縮比
            s.SetLevel(level);
            // 逐一將資料夾內的檔案抓出來壓縮,並寫入至目的檔(.ZIP)
            foreach (string filename in filenames)
            {
                ZipEntry entry = new ZipEntry(filename);
entry.IsUnicodeText = true;//避免壓縮後文件名亂碼
                s.PutNextEntry(entry);
                using (FileStream fs = File.OpenRead(filename))
                    StreamUtils.Copy(fs, s, buffer);
            }
            s.Flush();
            s.Close();
        }
    }

 #endregion

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鴨鴨仔 的頭像
    鴨鴨仔

    DUCK,Fearless!

    鴨鴨仔 發表在 痞客邦 留言(0) 人氣()