程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> .Net中使用壓縮和解壓縮(一)

.Net中使用壓縮和解壓縮(一)

編輯:.NET實例教程

前段時間,因項目需要.Net進行壓縮和解壓縮文件。在網上搜索了一圈,大部分人都提到了ICSharpCode.SharpZipLib.Zip。然後下載該開源代碼,再封裝了一下,以便項目需要。借此把該類代碼貼出,需要的朋友,可以看看,還請多提意見。

注:ICSharpCode.SharpZipLib.Zip 下載地址http://www.icsharpcode.Net/OpenSource/SharpZipLib/
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace ZipHelper
{
    /// <summary>
    /// Zip助手類,提供對ZIP格式文件的壓縮和解壓縮服務。
    /// </summary>
    /// <remarks></remarks>
    public class ZipHelper
    {
        #region 事件處理 提示用戶有相同文件時是否覆蓋
        //定義委托
        public delegate void ZipHelperEventHandler(ZipHelpEventArgs e);
        public event ZipHelperEventHandler ZipHelperProcessOverWrite;
        //事件調用
        public void OnZipHelperProcessOverWrite(ZipHelpEventArgs e)
        {
            if (ZipHelperProcessOverWrite != null)
                ZipHelperProcessOverWrite(e);
        }
        #region 事件參數類
        //事件參數類
        public class ZipHelpEventArgs : EventArgs
        {
            #region 變量
            /// <summary>
            /// 文件名
            /// </summary>
            private string _fileName;
            /// <summary>
            /// 是否覆蓋
            /// </summary>
            private bool _isOverWrite;
            #endregion
            #region 屬性
            public string FileName
            {
                get { return _fileName; }
                set { _fileName = value; }
            }
            /// <summary>
            /// 是否覆蓋
            /// </summary>
            public bool IsOverWrite
            {
                get { return _isOverWrite; }
                set { _isOverWrite = value; }
            }
            #endregion
            #region 構造
            public ZipHelpEventArgs()
            {
            }
            public ZipHelpEventArgs(string fileName,bool isOverWrite)
            {
                this.FileName = fileName;
                this.IsOverWrite = isOverWrite;
            }
            #endregion
        }
        #endregion
        #endregion
        #region 枚舉
        /// <summary>
        /// 用戶替換文件操作類型
        /// </summary>
        public enum ZipOverWrite
        {
            /// <summary>
            /// 提示用戶是否替換
            /// </summary>
            Prompt,
            /// <summary>
            /// 重不替換
            /// </summary>
            Never,
            /// <summary>
            /// 總是替換
            /// </summary>
            Always
        }
        #endregion
        #region 構造函數
     &nbsp;  /// <summary>
        /// 構造函數
        /// </summary>
        public ZipHelper()
        {
        }
        #endregion
        #region 方法
        #region 壓縮/解壓
        /// <summary>
        /// 壓縮文件
        /// </summary>
        /// <param name="fileList">文件路徑數組</param>
        /// <param name="targetFileName">壓縮後的文件路徑</param>
        public bool Compress(string[] fileList, string targetFileName)
        {
            bool rv = false;
            bool isOverWrite = false;
            ZipHelpEventArgs e = new ZipHelpEventArgs(targetFileName, isOverWrite);
            //如果已經存在目標文件,詢問用戶是否覆蓋
            if (File.Exists(targetFileName))
            {
                _ProcessOverwrite(e);
                if (e.IsOverWrite == false)
                    return false;
            }
            ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFileName));
 ;           try
            {
                foreach (string fName in fileList)
                {
                    FileInfo fi = new FileInfo(fName);
                    ZipEntry entry = new ZipEntry(fi.Name);
                    FileStream sReader = File.OpenRead(fName);
                    byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
                    sReader.Read(buff, 0, (int)sReader.Length);
                    entry.Size = sReader.Length;
                    sReader.Close();
                    entry.DateTime = fi.LastWriteTime;
                    zipOut.PutNextEntry(entry);
                    zipOut.Write(buff, 0, buff.Length);
                }
                zipOut.Finish();
      &nbsp;         zipOut.Close();
      &nbsp;         if (File.Exists(targetFileName))
                    rv = true;
                else
                    rv = false;
            }
            catch (Exception ex)
            {
                rv = false;
                throw ex;
            }
            return rv;
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved