Compression and Decompression of Files(C#)
今天遇到一個問題..就是要壓縮備份資料庫的mdb..google找了一些文章..用asp.net去試了一下..
參考小喵的文章 http://blog.blueshop.com.tw/topcat/archive/2008/02/04/54267.aspx 寫出一組的class..
因為小喵的是用vb寫的..我就把我的C#版丟出來..大家有需要去用ㄟ..
#region file相關方法 public class file { #region 壓縮資料夾 /// <summary> /// Compression of the directory /// </summary> /// <param name="source">ex:~/test</param> /// <param name="target">ex:~/test.zip</param> public void zipDirectory(string source, string target) { bool err = false; string msg = string.Empty; string sourcePath = HttpContext.Current.Server.MapPath(source); string targetPath = HttpContext.Current.Server.MapPath(target); if (new DirectoryInfo(sourcePath).Exists) { msg = string.Format("{0}資料夾位子錯誤", source); err = true; } if (!err) { //開始壓縮 new FastZip().CreateZip(targetPath, sourcePath, true, ""); } else { throw new Exception(msg); } } #endregion #region 壓縮檔案 /// <summary> /// Compression of the file /// 檔名加入時間戳記 /// </summary> /// <param name="source">ex:~/test.txt</param> public void zipFile(string source) { string sourcePath = HttpContext.Current.Server.MapPath(source); //加入時間戳記 string targetPath = string.Format(@"{0}\{1}{2:yyyyMMdd}.zip", Path.GetDirectoryName(sourcePath), Path.GetFileNameWithoutExtension(sourcePath), DateTime.Now); string target = source.Replace(Path.GetFileName(sourcePath), Path.GetFileName(targetPath)); zipFile(source, target); } /// <summary> /// Compression of the file /// </summary> /// <param name="source">ex:~/test.txt</param> /// <param name="target">ex:~/test.zip</param> public void zipFile(string source, string target) { bool err = false; string msg = string.Empty; string sourcePath = HttpContext.Current.Server.MapPath(source); string targetPath = HttpContext.Current.Server.MapPath(target); if (!new FileInfo(sourcePath).Exists) { msg = string.Format("{0}檔案位子錯誤", source); err = true; } if (!err) { //開始壓縮 using (ZipOutputStream zo = new ZipOutputStream(File.Create(targetPath))) { zo.SetLevel(9); using (FileStream fs = File.OpenRead(sourcePath)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry Entry = new ZipEntry(Path.GetFileName(sourcePath)); //Entry.ForceZip64();//檔案大於4G時使用 Entry.DateTime = DateTime.Now; Entry.Size = fs.Length; fs.Close(); zo.PutNextEntry(Entry); zo.Write(buffer, 0, buffer.Length); zo.Finish(); zo.Close(); } } } else { throw new Exception(msg); } } #endregion #region 解壓縮檔案 /// <summary> /// Decompression of the file /// </summary> /// <param name="source">ex:~/test.zip</param> /// <param name="targer">ex:~/test</param> public void unZip(string source, string target) { bool err = false; string msg = string.Empty; string sourcePath = HttpContext.Current.Server.MapPath(source); string targetPath = HttpContext.Current.Server.MapPath(target); if (!new FileInfo(sourcePath).Exists) { msg = string.Format("{0}檔案位子錯誤", source); err = true; } if (!err) { //開始解壓縮 new ICSharpCode.SharpZipLib.Zip.FastZip().ExtractZip(sourcePath, targetPath, ""); } else { throw new Exception(msg); } } #endregion } #endregion
不過測試結果..壓縮ICSharpCode.SharpZipLib壓縮解壓縮真的有點慢.. 不知道試我用隨身碟..
還是因為我電腦慢的關西.... 有任何問題..留言一下ㄅ..
ps:記得還是要去這網站下載ICSharpCode.SharpZipLib.dll這東西ㄟ..才可以跑..