一、 添加引用



.NET Framework 4.5 ZipArchive类压缩解压_ZipArchive转存失败重新上传取消


二、引入命名空间

using System.IO;

using System.IO.Compression;

三、实现代码

    #region .NET Framework 4.5 ZipArchive类 压缩、解压

    /// <summary>

    /// ZipArchive压缩

    /// </summary>

    /// <param name="str_ZipFilePath">压缩文件存放的路径(如:/UploadFiles/quber.zip)</param>

    /// <param name="str_OldFilePath">压缩文件的上级文件夹名称路径(如:/UploadFiles/)</param>

    /// <param name="bl_IsDeleteZip">是否首先删除已经存在的zip压缩包</param>

    public static bool ZAZIP(string str_ZipFilePath, string str_OldFilePath, bool bl_IsDeleteZip)

    {

        bool bl_Result = false;

        try

        {

            //删除已经存在的压缩包

            DeleteFile(str_ZipFilePath);

            //若压缩包的文件夹路径不存在,则创建

            string str_CreateZipFilePath = str_ZipFilePath.Substring(0, str_ZipFilePath.Length - (str_ZipFilePath.Length - str_ZipFilePath.LastIndexOf("/")));

            CreateFilePath(str_CreateZipFilePath);


            string[] str_AllFileName = GetAllFileName(str_OldFilePath);

            using (var zip = ZipFile.Open(System.Web.HttpContext.Current.Server.MapPath(str_ZipFilePath), ZipArchiveMode.Create))

            {

                if (str_AllFileName.Length > 0)

                {

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

                    {

                        zip.CreateEntryFromFile(System.Web.HttpContext.Current.Server.MapPath(str_OldFilePath) + str_AllFileName[i], str_AllFileName[i]);

                        bl_Result = true;

                    }

                }

            }

            return bl_Result;

        }

        catch (Exception)

        {

            return bl_Result;

        }

    }


    /// <summary>

    /// ZipArchive解压

    /// </summary>

    /// <param name="str_UNZipFilePath">解压的文件路径(如:/UploadFiles/quber.zip)</param>

    /// <param name="str_UNZipFileToPath">解压到的文件路径(如:/UploadFiles/UNZipFile/)</param>

    /// <param name="IsDeleteFile">是否删除解压到的文件夹中已经存在的文件</param>

    /// <returns></returns>

    public static bool UNZAZIP(string str_UNZipFilePath, string str_UNZipFileToPath, bool IsDeleteFile)

    {

        bool bl_Result = false;

        try

        {

            if (IsDeleteFile)

            {

                // 列出压缩压缩文件

                using (FileStream zipFileToOpen = new FileStream(System.Web.HttpContext.Current.Server.MapPath(str_UNZipFilePath), FileMode.Open))

                using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read))

                {

                    foreach (var zipArchiveEntry in archive.Entries)

                    {

                        DeleteFile(str_UNZipFileToPath + zipArchiveEntry.FullName);

                    }

                }

            }


            ZipFile.ExtractToDirectory(System.Web.HttpContext.Current.Server.MapPath(str_UNZipFilePath), System.Web.HttpContext.Current.Server.MapPath(str_UNZipFileToPath));


            bl_Result = true;

            return bl_Result;

        }

        catch (Exception)

        {

            return bl_Result;

        }

    }


    /// <summary>

    /// 获取文件夹下的所有文件名称

    /// </summary>

    /// <param name="str_FilePath">文件夹路径名称(如:/UploadFiles/)</param>

    /// <returns></returns>

    public static string[] GetAllFileName(string str_FilePath)

    {

        string[] str_AllFileName = { };

        try

        {

            DirectoryInfo di = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(str_FilePath));

            FileSystemInfo[] dis = di.GetFileSystemInfos();

            if (dis.Length > 0)

            {

                str_AllFileName = new string[dis.Length];

                int i_Count = 0;

                foreach (FileSystemInfo fitemp in dis)

                {

                    str_AllFileName[i_Count] = fitemp.Name;

                    i_Count++;

                }

            }

            return str_AllFileName;

        }

        catch (Exception)

        {

            return str_AllFileName;

        }

    }


    /// <summary>

    /// 创建文件夹

    /// </summary>

    /// <param name="str_FolderPath">/UploadFiles/</param>

    public static void CreateFilePath(string str_FolderPath)

    {

        string str_Path = System.Web.HttpContext.Current.Server.MapPath(str_FolderPath);

        if (!System.IO.Directory.Exists(str_Path))

            System.IO.Directory.CreateDirectory(str_Path);

    }


    /// <summary>

    /// 删除文件

    /// </summary>

    /// <param name="str_DataFilePath">/UploadFiles/name.txt</param>

    public static void DeleteFile(string str_DataFilePath)

    {

        if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(str_DataFilePath)))

            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(str_DataFilePath));

    }

    #endregion

四、 调用示例

bool bl_Result = ZAZIP("/UploadFiles/quber.zip", "/UploadFiles/", true);   


bool bl_Result = UNZAZIP("/UploadFiles/quber.zip", "/UploadFiles/UNZipFile/", true);