当前位置: 代码迷 >> 综合 >> c# 压缩文件、压缩多级目录及解压
  详细解决方案

c# 压缩文件、压缩多级目录及解压

热度:5   发布时间:2024-01-13 01:02:48.0

网上找了很多关于ICSharpCode.SharpZipLib.dll控件压缩都只支持一级目录,自己整理了一份支持压缩及解压缩多级目录,功能足够全,可以压缩大文件,在此之前先引用ICSharpCode.SharpZipLib.dll,ICSharpCode.SharpZipLib.dll下载


以下是Zip操作类:

using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Windows.Forms;

using System.IO;

using ICSharpCode.SharpZipLib;

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;

 

namespace WindowsFormsApplication1

{

    publicclassZipClass

    {

        publicstaticint avg = 1024 * 1024 * 100;//100MB写一次  

        #region 压缩文件 和 文件夹

        ///<summary>

        ///压缩文件 和 文件夹

        ///</summary>

        ///<param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>

        ///<param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>

        ///<returns>压缩是否成功</returns>

        publicstaticbool Zip(string FileToZip, string ZipedFile)

        {

            return Zip(FileToZip, ZipedFile, "");

        }

        ///<summary>

        ///压缩文件 和 文件夹,不压缩顶级目录

        ///</summary>

        ///<param name="FolderToZip">待压缩的文件夹,全路径格式</param>

        ///<param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>

        ///<returns>压缩是否成功</returns>

        publicstaticbool ZipNo(string FolderToZip, string ZipedFile)

        {

            if (!Directory.Exists(FolderToZip))

                returnfalse;

            if (ZipedFile == string.Empty)

            {

                //如果为空则文件名为待压缩的文件名加上.rar

                ZipedFile = FolderToZip + ".zip";

            }

            ZipOutputStream s = newZipOutputStream(File.Create(ZipedFile));

            s.SetLevel(6);

            string[] filenames = Directory.GetFiles(FolderToZip);

            ZipEntry entry = null;

            FileStream fs = null;

            Crc32 crc = newCrc32();

            foreach (string file in filenames)

            {

                //压缩文件

                fs = File.OpenRead(file);

                byte[] buffer = newbyte[avg];

                entry = newZipEntry(Path.GetFileName(file));

                entry.DateTime = DateTime.Now;

                entry.Size = fs.Length;

                s.PutNextEntry(entry);

                for (int i = 0; i < fs.Length; i += avg)

                {

                    if (i + avg > fs.Length)

                    {

                        //不足100MB的部分写剩余部分

                        buffer = newbyte[fs.Length - i];

                    }

                    fs.Read(buffer, 0, buffer.Length);

                    s.Write(buffer, 0, buffer.Length);

                }

            }

            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, "")) ;

            }

            s.Finish();

            s.Close();

            returntrue;

        }

        ///<summary>

        ///压缩文件 和 文件夹

        ///</summary>

        ///<param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>

        ///<param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>

        ///<param name="Password">压缩密码</param>

        ///<returns>压缩是否成功</returns>

        publicstaticbool Zip(string FileToZip, string ZipedFile, string Password)

        {

            if (Directory.Exists(FileToZip))

            {

                return ZipFileDictory(FileToZip, ZipedFile, Password);

            }

            elseif (File.Exists(FileToZip))

            {

                return ZipFile(FileToZip, ZipedFile, Password);

            }

            else

            {

                returnfalse;

            }

        }

        #endregion

 

        #region 解压

        ///<summary>  

        ///功能:解压zip格式的文件。  

        ///</summary>  

        ///<param name="zipFilePath">压缩文件路径,全路径格式</param>  

        ///<param name="unZipDir">解压文件存放路径,全路径格式,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>  

        ///<param name="err">出错信息</param>  

        ///<returns>解压是否成功</returns>  

        publicstaticbool UnZip(string zipFilePath, string unZipDir)

        {

            if (zipFilePath == string.Empty)

            {

                thrownew System.IO.FileNotFoundException("压缩文件不不能为空!");

            }

            if (!File.Exists(zipFilePath))

            {

                thrownew System.IO.FileNotFoundException("压缩文件: " + zipFilePath + " 不存在!");

            }

            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  

            if (unZipDir == string.Empty)

                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), "");

            if (!unZipDir.EndsWith("//"))

                unZipDir += "//";

            if (!Directory.Exists(unZipDir))

                Directory.CreateDirectory(unZipDir);

 

            try

            {

                using (ZipInputStream s = newZipInputStream(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 = newbyte[2048];

                                while (true)

                                {

                                    size = s.Read(data, 0, data.Length);

                                    if (size > 0)

                                    {

                                        streamWriter.Write(data, 0, size);

                                    }

                                    else

                                    {

                                        break;

                                    }

                                }

                            }

                        }

                    }//while  

                }

            }

            catch (Exception ex)

            {

                thrownewException(ex.Message);

            }

            returntrue;

        }//解压结束 

        #endregion

 

        #region 压缩目录

 

        ///<summary>

        ///压缩目录

        ///</summary>

        ///<param name="FolderToZip">待压缩的文件夹,全路径格式</param>

        ///<param name="ZipedFile">压缩后的文件名,全路径格式,如果为空则文件名为待压缩的文件名加上.rar</param>

        ///<returns></returns>

        privatestaticbool ZipFileDictory(string FolderToZip, string ZipedFile, string Password)

        {

            bool res;

            if (!Directory.Exists(FolderToZip))

                returnfalse;

            if (ZipedFile == string.Empty)

            {

                //如果为空则文件名为待压缩的文件名加上.rar

                ZipedFile = FolderToZip + ".zip";

            }

            ZipOutputStream s = newZipOutputStream(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 压缩文件

 

        ///<summary>

        ///压缩文件

        ///</summary>

        ///<param name="FileToZip">要进行压缩的文件名</param>

        ///<param name="ZipedFile">压缩后生成的压缩文件名,如果为空则文件名为待压缩的文件名加上.rar</param>

        ///<returns>压缩是否成功</returns>

        privatestaticbool ZipFile(string FileToZip, string ZipedFile, string Password)

        {

            //如果文件没有找到,则报错

            if (!File.Exists(FileToZip))

                thrownew System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");

            if (ZipedFile == string.Empty)

            {

                //如果为空则文件名为待压缩的文件名加上.rar

                ZipedFile = FileToZip + ".zip";

            }

            FileStream ZipFile = null;

            ZipOutputStream ZipStream = null;

            ZipEntry ZipEntry = null;

            bool res = true;

            ZipFile = File.Create(ZipedFile);

            ZipStream = newZipOutputStream(ZipFile);

            ZipEntry = newZipEntry(Path.GetFileName(FileToZip));

            ZipStream.PutNextEntry(ZipEntry);

            ZipStream.SetLevel(6);

            if (!string.IsNullOrEmpty(Password.Trim()))

                ZipStream.Password = Password.Trim();

            try

            {

                ZipFile = File.OpenRead(FileToZip);

                byte[] buffer = newbyte[avg];

                for (int i = 0; i < ZipFile.Length; i += avg)

                {

                    if (i + avg > ZipFile.Length)

                    {

                        //不足100MB的部分写剩余部分

                        buffer = newbyte[ZipFile.Length - i];

                    }

                    ZipFile.Read(buffer, 0, buffer.Length);

                    ZipStream.Write(buffer, 0, buffer.Length);

                }

            }

            catch (Exception ex)

            {

                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 递归压缩文件夹方法

        ///<summary>

        ///递归压缩文件夹方法

        ///</summary>

        ///<param name="FolderToZip"></param>

        ///<param name="s"></param>

        ///<param name="ParentFolderName"></param>

        privatestaticbool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)

        {

            bool res = true;

            string[] folders, filenames;

            ZipEntry entry = null;

            FileStream fs = null;

            Crc32 crc = newCrc32();

            try

            {

                //创建当前文件夹

                entry = newZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));  //加上 “/” 才会当成是文件夹创建

                s.PutNextEntry(entry);

                s.Flush();

                //先压缩文件,再递归压缩文件夹

                filenames = Directory.GetFiles(FolderToZip);

                foreach (string file in filenames)

                {

                    //打开压缩文件

                    fs = File.OpenRead(file);

                    byte[] buffer = newbyte[avg];

                    entry = newZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));

                    entry.DateTime = DateTime.Now;

                    entry.Size = fs.Length;

                    s.PutNextEntry(entry);

                    for (int i = 0; i < fs.Length; i += avg)

                    {

                        if (i + avg > fs.Length)

                        {

                            //不足100MB的部分写剩余部分

                            buffer = newbyte[fs.Length - i];

                        }

                        fs.Read(buffer, 0, buffer.Length);

                        s.Write(buffer, 0, buffer.Length);

                    }

                }

            }

            catch (Exception ex)

            {

                res = false;

            }

            finally

            {

                if (fs != null)

                {

                    fs.Close();

                    fs = null;

                }

                if (entry != null)

                    entry = null;

                GC.Collect();

                GC.Collect(1);

            }

            folders = Directory.GetDirectories(FolderToZip);

            foreach (string folder in folders)

            {

                if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))

                    returnfalse;

            }

            return res;

        }

 

        #endregion

    }

}

 

 调用示例:

 压缩目录:ZipClass.Zip(@"F:\a", "");//将F:\a压缩到当前文件夹a.zip

                   ZipClass.Zip(@"F:\a", "D:\a.zip");//将F:\a压缩到指定文件夹a.zip

 压缩文件:ZipClass.Zip(@"F:\a.txt", "");//将F:\a.txt压缩到当前文件夹a.txt.zip

                  ZipClass.Zip(@"F:\a.txt", "D:\a.txt.zip");//将F:\a.txt压缩到指定文件夹a.txt.zip

 解       压:ZipClass.UnZip(@"F:\a.zip", "");//将F:\a.zip解压到当前文件夹

                  ZipClass.UnZip(@"F:\a.zip", "D:\");//将F:\a.zip解压到指定文件夹