.NET Framework 4.5 ZipArchive类压缩解压

61 篇文章 6 订阅

 

 

 

一、 添加引用

 

1952028963588473902.pnguploading.4e448015.gif转存失败重新上传取消 .NET Framework 4.5 ZipArchive类压缩解压 - Quber - Quber
 

二、引入命名空间

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

 
参考资料:
http://www.cnblogs.com/haogj/archive/2012/11/17/2774733.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值