.Net中使用压缩和解压缩(二)

/// <summary>
        /// 压缩目录
        /// </summary>
        /// <param name="dir">目录路径</param>
        /// <param name="targetFileName">压缩后的文件路径</param>
        public bool Compress(string dir, 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 Directory.GetFiles(dir))
                {
                    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();
                zipOut.Close();
                if (File.Exists(targetFileName))
                    rv = true;
                else
                    rv = false;
            }
            catch (Exception ex)
            {
                rv = false;
                throw new System.NotImplementedException(ex.Message);
            }
            return rv;
        }
        /// <summary>
        /// 解压缩文件
        /// </summary>
        /// <remarks>支持递归解压缩(限1层递归),即压缩后文件中若出现zip文件,则继续解压缩。</remarks>       
        /// <param name="fileName">要解压缩的文件</param>
        /// <param name="targetDir">解压缩目录</param>
        /// <param name="allowRecursive">是否允许递归解压缩</param>       
        public bool Decompress(string fileName, string targetDir, bool allowRecursive)
        {
            bool rv = true;
            FastZip fz = new FastZip();
            fz.RestoreDateTimeOnExtract = true;  //保留原文件最后修改时间
            try
            {
                fz.ExtractZip(fileName, targetDir, "");
            }
            catch (Exception ex)
            {
                rv = false;
                throw ex;
            }
            if (rv == false)
                return rv;
            DirectoryInfo dr = new DirectoryInfo(targetDir);
            FileInfo[] fis = dr.GetFiles();
            string tempex;
            string tempdir;
            string tempname;
            try
            {
                if (allowRecursive)
                {
                    foreach (FileInfo fi in fis)
                    {
                        tempex = fi.Extension;
                        if (tempex == ".zip")      //解压内层zip文件,放入以现有文件名称命名的文件夹中
                        {
                            tempname = fi.Name.Substring(0, fi.Name.Length - tempex.Length);
                            tempdir = Path.Combine(fi.DirectoryName, tempname);
                            if (Directory.Exists(tempdir))  //如果在解压内层的zip文件时,遇到已经存在的文件夹,将以文件名[1]命名文件夹
                            {
                                tempdir = Path.Combine(fi.DirectoryName, tempname + "[1]");
                            }
                            fz.ExtractZip(fi.FullName, tempdir, "");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                rv = false;
                throw ex;
            }
            return rv;
        }
        /// <summary>
        /// 解压缩文件
        /// </summary>
        /// <remarks>支持递归解压缩(限1层递归),即压缩后文件中若出现zip文件,则继续解压缩。</remarks>       
        /// <param name="fileName">要解压缩的文件</param>
        /// <param name="targetDir">解压缩目录</param>
        /// <param name="allowRecursive">是否允许递归解压缩</param>
        /// <param name="overWrite">解压缩时,遇到已经存在的文件是否提示用户替换 ZipOverWrite 枚举类型</param>
        public bool Decompress(string fileName, string targetDir, bool allowRecursive, ZipOverWrite overWrite)
        {
            bool rv = true;
            FastZip fz = new FastZip();
            //根据用户设置 遇到重文件时的操作,设置FastZip的OverWrite
            int oi = (int)overWrite;
            FastZip.Overwrite ov = FastZip.Overwrite.Prompt;
            switch (oi)
            {
                case 0:
                    ov = FastZip.Overwrite.Prompt;
                    break;
                case 1:
                    ov = FastZip.Overwrite.Never;
                    break;
                case 2:
                    ov = FastZip.Overwrite.Always;
                    break;
            }
            try
            {
                fz.ExtractZip(fileName, targetDir, ov, _ProcessOverwrite, "", "", true);
            }
            catch (Exception ex)
            {
                rv = false;
                throw new System.NotImplementedException(ex.Message);
            }
            if (rv == false)
                return rv;
            DirectoryInfo dr = new DirectoryInfo(targetDir);
            FileInfo[] fis = dr.GetFiles();
            string tempex;
            string tempdir;
            string tempname;
            try
            {
                if (allowRecursive)   //允许递归
                {
                    foreach (FileInfo fi in fis)
                    {
                        tempex = fi.Extension;
                        if (tempex.ToLower() == ".zip")    //解压内层zip文件,放入以现有文件名称命名的文件夹中
                        {
                            tempname = fi.Name.Substring(0, fi.Name.Length - tempex.Length);
                            tempdir = Path.Combine(fi.DirectoryName, tempname);
                            if (Directory.Exists(tempdir))  //如果在解压内层的zip文件时,遇到已经存在的文件夹,将以文件名[1]命名文件夹
                            {
                                tempdir = Path.Combine(fi.DirectoryName, tempname + "[1]");
                            }
                            fz.ExtractZip(fi.FullName, tempdir, ov, _ProcessOverwrite, "", "", true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                rv = false;
                throw ex;
            }
            return rv;
        }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值