遞迴壓縮檔

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.DirectoryServices;

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;

 public class ZipHelper
 {

    .........

      #region 壓縮檔夾,支援遞迴
       
        /// <summary>
        /// 壓縮檔夾
        /// </summary>
        /// <param name="dir">待壓縮的檔案夾</param>
        /// <param name="targetFileName">壓縮後檔案路徑(包括檔案名稱)</param>
        /// <param name="recursive">是否遞迴壓縮</param>
        /// <returns></returns>
        public static bool Compress(string dir, string targetFileName,bool recursive)
        {
            //如果已經存在目標檔案,詢問使用者是否覆蓋
            if (File.Exists(targetFileName))
            {
                if (!_ProcessOverwrite(targetFileName))
                    return false;
            }

            if (recursive == false)
                return Compress(dir, targetFileName);

          
            FileStream ZipFile;
            ZipOutputStream ZipStream;

            //open
            ZipFile = File.Create(targetFileName);
            ZipStream = new ZipOutputStream(ZipFile);

            if (dir != String.Empty)
            {
                _CompressFolder(dir, ZipStream, dir);
            }

            //close
            ZipStream.Finish();
            ZipStream.Close();

            if (File.Exists(targetFileName))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 壓縮某個子檔案夾
        /// </summary>
        /// <param name="basePath"></param>
        /// <param name="zips"></param>
        /// <param name="zipfolername"></param>    
        private static void _CompressFolder(string basePath, ZipOutputStream zips, string zipfolername)
        {
            if (File.Exists(basePath))
            {
                _AddFile(basePath, zips, zipfolername);
                return;
            }
            string[] names = Directory.GetFiles(basePath);
            foreach (string fileName in names)
            {
                _AddFile(fileName, zips, zipfolername);
            }

            names = Directory.GetDirectories(basePath);
            foreach (string folderName in names)
            {
                _CompressFolder(folderName, zips, zipfolername);
            }

        }

        /// <summary>
        /// 壓縮某個子檔案
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="zips"></param>
        /// <param name="zipfolername"></param>
        private static void _AddFile(string fileName, ZipOutputStream zips, string zipfolername)
        {
            if (File.Exists(fileName))
            {
                _CreateZipFile(fileName,zips,zipfolername);
            }
        }

        /// <summary>
        /// 壓縮單獨檔案
        /// </summary>
        /// <param name="FileToZip"></param>
        /// <param name="zips"></param>
        /// <param name="zipfolername"></param>
        private static void _CreateZipFile(string FileToZip, ZipOutputStream zips,string zipfolername)
        {
            try
            {
                FileStream StreamToZip = new FileStream(FileToZip, FileMode.Open, FileAccess.Read);
                string temp = FileToZip;
                string temp1 = zipfolername;
                if (temp1.Length > 0)
                {
                    int i = temp1.LastIndexOf('\\') + 1;
                    int j = temp.Length - i;
                    temp = temp.Substring(i, j);
                }
                ZipEntry ZipEn = new ZipEntry(temp);

                zips.PutNextEntry(ZipEn);
                byte[] buffer = new byte[16384];
                System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
                zips.Write(buffer, 0, size);
                try
                {
                    while (size < StreamToZip.Length)
                    {
                        int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                        zips.Write(buffer, 0, sizeRead);
                        size += sizeRead;
                    }
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }

                StreamToZip.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #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.