【原创】ASP.NET C# 压缩和解压缩文件、文件夹函数(测试通过)


//注意:本文为博主原创,转载请注明出处,谢谢合作!

 #region 文件下载函数DownFile
    /// <summary>
    /// 下载文件方法
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>       
    public static bool DownFile(string filePath)
    {
        try
        {
            if (File.Exists(filePath))
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filePath);
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.WriteFile(filePath);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.Close();
                HttpContext.Current.Response.End();
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
    #endregion

    #region 文件夹压缩解压缩
    /// <summary>
    /// 使用WinRAR解压缩文件
    /// </summary>
    /// <param name="srcFilePath">要压缩的文件</param>
    /// <param name="aimFilePath">保存压缩文件的路径</param>
    /// <returns>压缩成功返回true,否则返回false</returns>
    public static bool CreatRAR(string srcFilePath, string aimFilePath)
    {
        try
        {
            if (System.IO.File.Exists(aimFilePath))
                System.IO.File.Delete(aimFilePath);
            if (!System.IO.File.Exists(srcFilePath))
                return false;
            System.Diagnostics.Process ProcessRar = new System.Diagnostics.Process();
            ProcessRar.StartInfo.FileName = "Winrar.exe";
            ProcessRar.StartInfo.CreateNoWindow = true;
            ProcessRar.StartInfo.Arguments = " a -r " + aimFilePath + " " + srcFilePath;
            ProcessRar.Start();
            ProcessRar.WaitForExit();

            bool bln = false;
            if (ProcessRar.HasExited)
                if (ProcessRar.ExitCode == 0)
                    bln = true;
            ProcessRar.Close();

            return bln;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 使用WinRAR解压缩文件
    /// </summary>
    /// <param name="aimPath">解压文件存储的路径</param>
    /// <param name="srcFilePath">压缩文件的完整地址</param>
    /// <returns>解压完成返回true,否则返回false</returns>
    public static bool UnRAR(string srcFilePath, string aimPath)
    {
        try
        {
            if (System.IO.File.Exists(srcFilePath))
            {
                string rarName = srcFilePath.Split('\\')[srcFilePath.Split('\\').Length - 1];
                string rarPath = srcFilePath.Remove(srcFilePath.LastIndexOf(rarName) - 1);

                Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
                System.Object regvalue = regkey.GetValue("");
                string rarexe = regvalue.ToString();
                regkey.Close();
                rarexe = rarexe.Substring(1, rarexe.Length - 7);

                if (!System.IO.File.Exists(aimPath))
                    System.IO.Directory.CreateDirectory(aimPath);

                //解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压文件->目标路径(aimPath)
                string cmd = string.Format("x {0} {1} -y ", rarName, aimPath);

                System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
                startinfo.FileName = rarexe;
                startinfo.Arguments = cmd;
                startinfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startinfo.WorkingDirectory = rarPath;

                System.Diagnostics.Process ProcessRar = new System.Diagnostics.Process();
                ProcessRar.StartInfo = startinfo;
                ProcessRar.Start();
                ProcessRar.WaitForExit();

                bool bln = false;
                if (ProcessRar.HasExited)
                    if (ProcessRar.ExitCode == 0)
                        bln = true;
                ProcessRar.Close();

                return bln;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
    #endregion

    /*
    * 限制
    路径名长度小于 259 个字符。
    最大的压缩文件注释的长度是62000字节。
    命令限制:
    命令 'd','u','f','c','cf' 不能用于分卷压缩文件。
    命令 'a' 不能用来更新分卷压缩文件,只能用来创建。

    * 返回值
    成功操作后返回 0 。非 0 返回码意味着操作由于某种错误被取消:
    255   用户中断         用户中断操作
    9   创建错误         创建文件错误
    8   内存错误         没有足够的内存进行操作
    7   用户错误         命令行选项错误
    6   打开错误         打开文件错误
    5   写错误           写入磁盘错误
    4   被锁定压缩文件   试图修改先前使用 'k' 命令锁定的压缩文件
    3   CRC 错误         解压缩时发生一个 CRC 错误,当密码不对时返回3
    2   致命错误         发生一个致命错误
    1   警告             没有发生致命错误
    0   成功             操作成功
   
    *RAR参数:

    一、压缩命令
    1、将temp.txt压缩为temp.rar 参数为: rar a temp.rar temp.txt
    2、将当前目录下所有文件压缩到temp.rar 参数为: rar a temp.rar *.*
    3、将当前目录下所有文件及其所有子目录压缩到temp.rar 参数为: rar a temp.rar *.* -r
    4、将当前目录下所有文件及其所有子目录压缩到temp.rar,并加上密码123 参数为: rar a temp.rar *.* -r -p123

    二、解压命令
    1、将temp.rar解压到c:\temp目录rar e temp.rar c:\temprar e *.rar c:\temp(支持批量操作)
    2、将temp.rar解压到c:\temp目录,并且解压后的目录结构和temp.rar中的目录结构一


    压缩目录test及其子目录的文件内容
    Wzzip test.zip test -r -P
    WINRAR A test.rar test -r

    删除压缩包中的*.txt文件
    Wzzip test.zip *.txt -d
    WinRAR d test.rar *.txt


    刷新压缩包中的文件,即添加已经存在于压缩包中但更新的文件
    Wzzip test.zip test -f
    Winrar f test.rar test


    更新压缩包中的文件,即添加已经存在于压缩包中但更新的文件以及新文件
    Wzzip test.zip test -u
    Winrar u test.rar test


    移动文件到压缩包,即添加文件到压缩包后再删除被压缩的文件
    Wzzip test.zip -r -P -m
    Winrar m test.rar test -r


    添加全部 *.exe 文件到压缩文件,但排除有 a或b
    开头名称的文件
    Wzzip test *.exe -xf*.* -xb*.*
    WinRAR a test *.exe -xf*.* -xb*.*


    加密码进行压缩
    Wzzip test.zip test
    -s123。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到+号标记(附图1)。
    WINRAR A test.rar test -p123
    -r。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到*号标记(附图2)。


    按名字排序、以简要方式列表显示压缩包文件
    Wzzip test.zip -vbn
    Rar l test.rar


    锁定压缩包,即防止未来对压缩包的任何修改
    无对应命令
    Winrar k test.rar


    创建360kb大小的分卷压缩包
    无对应命令
    Winrar a -v360 test


    带子目录信息解压缩文件
    Wzunzip test -d
    Winrar x test -r


    不带子目录信息解压缩文件
    Wzunzip test
    Winrar e test


    解压缩文件到指定目录,如果目录不存在,自动创建
    Wzunzip test newfolder
    Winrar x test newfolder


    解压缩文件并确认覆盖文件
    Wzunzip test -y
    Winrar x test -y


    解压缩特定文件
    Wzunzip test *.txt
    Winrar x test *.txt


    解压缩现有文件的更新文件
    Wzunzip test -f
    Winrar x test -f


    解压缩现有文件的更新文件及新文件
    Wzunzip test -n
    Winrar x test -u


    批量解压缩文件
    Wzunzip *.zip
    WinRAR e *.rar
   
    */

原文地址:https://www.cnblogs.com/cosiray/p/1555430.html