C#壓縮解壓幫助類
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;namespace GzRMIS.Main.Business
{
public class ZipHelper
{
/// <summary>
/// 存放待壓縮的文件的絕對路徑
/// </summary>
private List<string> AbsolutePaths { set; get; }
public string errorMsg { set; get; }public ZipHelper() { errorMsg = ""; AbsolutePaths = new List<string>(); } /// <summary> /// 添加壓縮文件或文件夾 /// </summary> /// <param name="_fileAbsolutePath">文件或文件夾的絕對路徑</param> public void AddFile(string _fileAbsolutePath) { AbsolutePaths.Add(_fileAbsolutePath); } /// <summary> /// 壓縮文件或者文件夾 /// </summary> /// <param name="_depositPath">壓縮后文件的存放路徑 如C:\\windows\abc.zip</param> /// <param name="_Level">壓縮級別0~9,數字越大壓縮率越高,默認為5</param> /// <returns></returns> public bool CompressionZip(string _depositPath,int _Level=5) { bool result = true; FileStream fs = null; try { ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath)); ComStream.SetLevel(_Level); //壓縮等級 foreach (string path in AbsolutePaths) { //如果是目錄 if (Directory.Exists(path)) { ZipFloder(path, ComStream, path); } else if (File.Exists(path))//如果是文件 { fs = File.OpenRead(path); byte[] bts = new byte[fs.Length]; fs.Read(bts, 0, bts.Length); ZipEntry ze = new ZipEntry(new FileInfo(path).Name); ComStream.PutNextEntry(ze); //為壓縮文件流提供一個容器 ComStream.Write(bts, 0, bts.Length); //寫入字節 } } ComStream.Finish(); // 結束壓縮 ComStream.Close(); } catch (Exception ex) { if (fs != null) { fs.Close(); } errorMsg = ex.Message; result = false; } return result; } //壓縮文件夾 private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath) { foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos()) { if (Directory.Exists(item.FullName)) { ZipFloder(_OfloderPath, zos, item.FullName); } else if (File.Exists(item.FullName))//如果是文件 { DirectoryInfo ODir = new DirectoryInfo(_OfloderPath); string fullName2 = new FileInfo(item.FullName).FullName; string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//獲取相對目錄 FileStream fs = File.OpenRead(fullName2); byte[] bts = new byte[fs.Length]; fs.Read(bts, 0, bts.Length); ZipEntry ze = new ZipEntry(path); zos.PutNextEntry(ze); //為壓縮文件流提供一個容器 zos.Write(bts, 0, bts.Length); //寫入字節 } } } /// <summary> /// 解壓 /// </summary> /// <param name="_depositPath">壓縮文件路徑</param> /// <param name="_floderPath">解壓的路徑</param> /// <returns></returns> public bool DeCompressionZip(string _depositPath, string _floderPath) { bool result = true; FileStream fs = null; try { ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath)); ZipEntry ze = InpStream.GetNextEntry();//獲取壓縮文件中的每一個文件 Directory.CreateDirectory(_floderPath);//創建解壓文件夾 while (ze != null)//如果解壓完ze則是null { if (ze.IsFile)//壓縮zipINputStream里面存的都是文件。帶文件夾的文件名字是文件夾\\文件名 { string[] strs = ze.Name.Split('\\');//如果文件名中包含’\\‘則表明有文件夾 if (strs.Length > 1) { //兩層循環用于一層一層創建文件夾 for (int i = 0; i < strs.Length - 1; i++) { string floderPath = _floderPath; for (int j = 0; j < i; j++) { floderPath = floderPath + "\\" + strs[j]; } floderPath = floderPath + "\\" + strs[i]; Directory.CreateDirectory(floderPath); } } fs = new FileStream(_floderPath + "\\" + ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//創建文件 //循環讀取文件到文件流中 while (true) { byte[] bts = new byte[1024]; int i = InpStream.Read(bts, 0, bts.Length); if (i > 0) { fs.Write(bts, 0, i); } else { fs.Flush(); fs.Close(); break; } } } ze = InpStream.GetNextEntry(); } } catch (Exception ex) { if (fs != null) { fs.Close(); } errorMsg = ex.Message; result = false; } return result; } }
} </pre>
本文由用戶 g3mc 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!