c#压缩文件夹

来源:互联网 发布:管理自己的淘宝店铺 编辑:程序博客网 时间:2024/06/18 15:34

压缩文件夹网上找了好久,终于找到了。(可以添加条件,挑选要的文件类型进行压缩。)

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication2.selectSDF.tool.zipFile
{
    class ZipHelper2
    {
        #region ZipFileDictory
        /// <summary>
        /// 递归压缩文件夹方法
        /// </summary>
        /// <param name="FolderToZip"></param>
        /// <param name="s"></param>
        /// <param name="ParentFolderName"></param>
        private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
        {
            bool res = true; 
            ZipEntry entry = null;
            FileStream fs = null;
            Crc32 crc = new Crc32();
            try
            {
                //创建当前文件夹
                entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));  //加上 “/” 才会当成是文件夹创建
                s.PutNextEntry(entry);
                s.Flush();
                //先压缩文件,再递归压缩文件夹 
                string[] filenames = Directory.GetFiles( FolderToZip);
                foreach (string file in filenames)
                {
                    //--------判断是想要的文件后缀类型时,下手。------------
                    string extension = Path.GetExtension(file);//后缀名".xlsx|.log|.html|.csv"
                    if (extension == ".csv")
                    {
                        //打开压缩文件
                        fs = File.OpenRead(file);
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);


                        entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
                        entry.DateTime = DateTime.Now;
                        entry.Size = fs.Length;


                        fs.Close();


                        crc.Reset();
                        crc.Update(buffer);


                        entry.Crc = crc.Value;
                        s.PutNextEntry(entry);
                        s.Write(buffer, 0, buffer.Length);
                    }
                }
            }
            catch
            {
                res = false;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                if (entry != null)
                    entry = null;
                GC.Collect();
                GC.Collect(1);
            }
            string[] folders = Directory.GetDirectories(FolderToZip);
            foreach (string folder in folders)
            {
                if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName,Path.GetFileName(FolderToZip))))
                    return false;
            }
            return res;
        }
        #endregion




        #region ZipFileDictory
        /// <summary>
        ///  压缩目录
        /// </summary>
        /// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
        /// <param name="ZipedFile">压缩后的文件名,全路径格式</param>
        /// <returns></returns>
        private static bool ZipFileDictory(string FolderToZip, string ZipedFile, string Password)
        {
            bool res;
            if (!Directory.Exists(FolderToZip))
                return false;
            ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
            s.SetLevel(6);
            if (!string.IsNullOrEmpty(Password.Trim()))
                s.Password = Password.Trim();
            res = ZipFileDictory(FolderToZip, s, "");
            s.Finish();
            s.Close();
            return res;
        }
        #endregion


        #region ZipFile
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="FileToZip">要进行压缩的文件名</param>
        /// <param name="ZipedFile">压缩后生成的压缩文件名</param>
        /// <returns></returns>
        private static bool ZipFile(string FileToZip, string ZipedFile, string Password)
        {
            //如果文件没有找到,则报错
            if (!File.Exists(FileToZip))
                throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
            //FileStream fs = null;
            FileStream ZipFile = null;
            ZipOutputStream ZipStream = null;
            ZipEntry ZipEntry = null;
            bool res = true;
            try
            {
                ZipFile = File.OpenRead(FileToZip);
                byte[] buffer = new byte[ZipFile.Length];
                ZipFile.Read(buffer, 0, buffer.Length);
                ZipFile.Close();
                ZipFile = File.Create(ZipedFile);
                ZipStream = new ZipOutputStream(ZipFile);
                if (!string.IsNullOrEmpty(Password.Trim()))
                    ZipStream.Password = Password.Trim();
                ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
                ZipStream.PutNextEntry(ZipEntry);
                ZipStream.SetLevel(6);
                ZipStream.Write(buffer, 0, buffer.Length);
            }
            catch
            {
                res = false;
            }
            finally
            {
                if (ZipEntry != null)
                {
                    ZipEntry = null;
                }
                if (ZipStream != null)
                {
                    ZipStream.Finish();
                    ZipStream.Close();
                }
                if (ZipFile != null)
                {
                    ZipFile.Close();
                    ZipFile = null;
                }
                GC.Collect();
                GC.Collect(1);
            }
            return res;
        }
        #endregion


        #region Zip
        /// <summary>
        /// 压缩文件 和 文件夹
        /// </summary>
        /// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
        /// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
        /// <param name="Password">压缩密码</param>
        /// <returns></returns>
        public static bool Zip(string FileToZip, string ZipedFile, string Password)
        {
            if (Directory.Exists(FileToZip))
            {
                return ZipFileDictory(FileToZip, ZipedFile, Password);
            }
            else if (File.Exists(FileToZip))
            {   
                return ZipFile(FileToZip, ZipedFile, Password); 
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 压缩文件 和 文件夹
        /// </summary>
        /// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
        /// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
        /// <returns></returns>
        public static bool Zip(string FileToZip, string ZipedFile)
        {
            return Zip(FileToZip, ZipedFile, "");
        }
        #endregion
    }

}

本人已试过,这个很好。

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 山药玉米排骨汤 荨麻疹能吃山药吗 山药炒黑木耳 淮山是山药吗 胡萝卜山药排骨汤 乌鸡山药汤的做法 山药和胡萝卜 山药可以减肥吗 山药可以炖排骨吗 淮山是什么 淮山的功效 淮山 淮山的功效与作用 淮山怎么做好吃 怀山 淮山的做法大全家常 淮山药 淮山药图片 淮山怎么炒 淮山的做法 淮山汤的做法 淮山怎么炒好吃 三药 拔丝 拔丝香蕉的做法 拔丝香蕉 拔丝芋头 拔丝红薯 拔丝芋头的做法 拔丝红薯的做法 拔丝鸡蛋 淮山不能与什么同吃 炒淮山的做法 淮山不能和什么一起吃 玉米胡萝卜猪骨汤 孕妇能不能吃芋头 妈妈我要吃烤山药 什么人不能吃山药 孕妇能不能吃山药 淮山药和铁棍山药 寻麻疹可以吃山药吗