欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

C#压缩解压帮助类

程序员文章站 2022-03-27 10:13:03
...
using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Web;  
using ICSharpCode.SharpZipLib.Zip;  
   
namespace GzRMIS.Main.Business  
{  
    public class ZipHelper  
    {  
        ///   
        /// 存放待压缩的文件的绝对路径  
        ///   
        private List AbsolutePaths { set; get; }  
        public string errorMsg { set; get; }  
   
        public ZipHelper()  
        {  
            errorMsg = "";  
            AbsolutePaths = new List();  
        }  
        ///   
        /// 添加压缩文件或文件夹  
        ///   
        /// 文件或文件夹的绝对路径  
        public void AddFile(string _fileAbsolutePath)  
        {  
            AbsolutePaths.Add(_fileAbsolutePath);  
        }  
        ///   
        /// 压缩文件或者文件夹  
        ///   
        /// 压缩后文件的存放路径   如C:\\windows\abc.zip  
        ///  压缩级别0~9,数字越大压缩率越高,默认为5  
        ///   
        public bool CompressionZip(string _depositPath,int _Level=5)  
        {  
            bool result = true;  
            FileStream fs = null;  
            try 
            {  
                ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));  
                ComStream.SetLevel(_Level);      //压缩等级  
                foreach (string path in AbsolutePaths)  
                {  
                    //如果是目录  
                    if (Directory.Exists(path))  
                    {  
                        ZipFloder(path, ComStream, path);  
                    }  
                    else if (File.Exists(path))//如果是文件  
                    {  
                        fs = File.OpenRead(path);  
                        byte[] bts = new byte[fs.Length];  
                        fs.Read(bts, 0, bts.Length);  
                        ZipEntry ze = new ZipEntry(new FileInfo(path).Name);  
                        ComStream.PutNextEntry(ze);             //为压缩文件流提供一个容器  
                        ComStream.Write(bts, 0, bts.Length);  //写入字节  
                    }  
                }  
                ComStream.Finish(); // 结束压缩  
                ComStream.Close();  
            }  
            catch (Exception ex)  
            {  
                if (fs != null)  
                {  
                    fs.Close();  
                }  
                errorMsg = ex.Message;  
                result = false;  
            }  
            return result;  
        }  
        //压缩文件夹  
        private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)  
        {  
            foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())  
            {  
                if (Directory.Exists(item.FullName))  
                {  
                    ZipFloder(_OfloderPath, zos, item.FullName);  
                }  
                else if (File.Exists(item.FullName))//如果是文件  
                {  
                    DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);  
                    string fullName2 = new FileInfo(item.FullName).FullName;  
                    string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录  
                    FileStream fs = File.OpenRead(fullName2);  
                    byte[] bts = new byte[fs.Length];  
                    fs.Read(bts, 0, bts.Length);  
                    ZipEntry ze = new ZipEntry(path);  
                    zos.PutNextEntry(ze);             //为压缩文件流提供一个容器  
                    zos.Write(bts, 0, bts.Length);  //写入字节  
                }  
            }  
        }  
        ///   
        /// 解压  
        ///   
        /// 压缩文件路径  
        /// 解压的路径  
        ///   
        public bool DeCompressionZip(string _depositPath, string _floderPath)  
        {  
            bool result = true;  
            FileStream fs = null;  
            try 
            {  
                ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath));  
                ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件  
                Directory.CreateDirectory(_floderPath);//创建解压文件夹  
                while (ze != null)//如果解压完ze则是null  
                {  
                    if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名  
                    {  
                        string[] strs = ze.Name.Split('\\');//如果文件名中包含’\\‘则表明有文件夹  
                        if (strs.Length > 1)  
                        {  
                            //两层循环用于一层一层创建文件夹  
                            for (int i = 0; i  0)  
                            {  
                                fs.Write(bts, 0, i);  
                            }  
                            else 
                            {  
                                fs.Flush();  
                                fs.Close();  
                                break;  
                            }  
                        }  
                    }  
                    ze = InpStream.GetNextEntry();  
                }  
            }  
            catch (Exception ex)  
            {  
                if (fs != null)  
                {  
                    fs.Close();  
                }  
                errorMsg = ex.Message;  
                result = false;  
            }  
            return result;  
        }  
   
    }  
}