C#第三方zip解壓壓縮公用程式,帶案例源碼

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;  //開源工具,可免費下載::

//http://files.cnblogs.com/xiaowei0705/SharpZipLib_0860_Bin.zip
using System.IO; 

namespace Package
{
    class Class1
    {
        #region 加壓解壓方法
        /// <summary>  
        /// 功能:壓縮檔(暫時只壓縮檔夾下一級目錄中的檔案,檔案夾及其子級被忽略)  
        /// </summary>  
        /// <param name="dirPath">被壓縮的檔案夾夾路徑</param>  
        /// <param name="zipFilePath">產生壓縮檔的路徑,為空白則預設與被壓縮檔夾同一級目錄,名稱為:檔案夾名+.zip</param>  
        /// <param name="err">出錯資訊</param>  
        /// <returns>是否壓縮成功</returns>  
        public bool ZipFile(string dirPath, string zipFilePath, out string err)
        {
            err = "";
            if (dirPath == string.Empty)
            {
                err = "要壓縮的檔案夾不可為空!";
                return false;
            }
            if (!Directory.Exists(dirPath))
            {
                err = "要壓縮的檔案夾不存在!";
                return false;
            }
            //壓縮檔名為空白時使用檔案夾名+.zip  
            if (zipFilePath == string.Empty)
            {
                if (dirPath.EndsWith("\\"))
                {
                    dirPath = dirPath.Substring(0, dirPath.Length - 1);
                }
                zipFilePath = dirPath + ".zip";
            }

            try
            {
                string[] filenames = Directory.GetFiles(dirPath);
                using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
                {
                    s.SetLevel(9);
                    byte[] buffer = new byte[4096];
                    foreach (string file in filenames)
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }
                    s.Finish();
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
            return true;
        }

        /// <summary>  
        /// 功能:解壓zip格式的檔案。  
        /// </summary>  
        /// <param name="zipFilePath">壓縮檔路徑</param>  
        /// <param name="unZipDir">解壓檔案存放路徑,為空白時預設與壓縮檔同一級目錄下,跟壓縮檔同名的檔案夾</param>  
        /// <param name="err">出錯資訊</param>  
        /// <returns>解壓是否成功</returns>  
        public bool UnZipFile(string zipFilePath, string unZipDir, out string err)
        {
            err = "";
            if (zipFilePath == string.Empty)
            {
                err = "壓縮檔不可為空!";
                return false;
            }
            if (!File.Exists(zipFilePath))
            {
                err = "壓縮檔不存在!";
                return false;
            }
            //解壓檔案夾為空白時預設與壓縮檔同一級目錄下,跟壓縮檔同名的檔案夾  
            if (unZipDir == string.Empty)
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
            if (!unZipDir.EndsWith("\\"))
                unZipDir += "\\";
            if (!Directory.Exists(unZipDir))
                Directory.CreateDirectory(unZipDir);

            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {

                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        if (directoryName.Length > 0)
                        {
                            Directory.CreateDirectory(unZipDir + directoryName);
                        }
                        if (!directoryName.EndsWith("\\"))
                            directoryName += "\\";
                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                            {

                                int size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }//while  
                }
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
            return true;
        }//解壓結束 
        #endregion

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.