C#调用winrar压缩文件夹

作者&投稿:习先 (若有异议请与网页底部的电邮联系)
怎样用C#直接调用Winrar把多个文件打包成*.rar压缩包?~

服务器用WScript.Shell组件调用winrar中的rar.exe,在代码的同目录要上传cmd.exe和rar.exe

用法:rar.ASP?path=要压缩的文件夹&rarname=压缩后的文件名&includefolder=包括子文件夹就不会空

<%
'用法http://xx/rar.ASP?path=要压缩的文件夹&rarname=压缩后的文件名&includefolder=包插子文件夹就不会空,
on error resume next
AppPath=server.mappath(".")&"\" '存放RAR.EXE和CMD.EXE的路径
zipfolder=server.mappath(request("path"))&"\" '要压缩的文件或文件夹
rarfile = request("rarname")
thispath = server.mappath(".")&"\"
if zipfolder"" and rarfile"" then
Set Shell = Server.CreateObject("WScript.Shell")
if err.number0 then
response.write "服务器不支持"
response.end
end if
if request("includefolder")"" then
cmd= thispath&"cmd.exe /c "&thispath&"rar.exe a -ep1 -r -t -o+ "&thispath&rarfile&" "& zipfolder &""
else
cmd= thispath&"cmd.exe /c "&thispath&"rar.exe a -ep1 -t -o+ "&thispath&rarfile&" "& zipfolder &""
end if
'response.write cmd
RetCode = Shell.Run(cmd,1, True)
response.write ""&rarfile&""
if err.number0 then
response.write ""&rarfile&""
else
response.write err.description
end if
else
response.write "没有参数"
end if
%>

"a-as"是压缩命令参数,这中间其实包含了2个命令参数a:添加指定的文件和文件夹到压缩文件中-as:同步压缩文件内容如果此开关使用于压缩时,在当前添加的文件列表中不存在的被压缩文件,将会从压缩文件中删除。它可以很方便的与-u开关结合,用来同步压缩文件和压缩的文件夹的内容。举例来说,在下面命令之后:WinRARa-u-asbackupsources\*.cpp压缩文件backup.rar将只会包含源文件夹的*.cpp文件,其它的全部文件将会从压缩文件中删除。它看起来就好象创建新压缩文件,但有一个重要的不同:如果从上次备份后没有修改过的文件,这项操作会比创建新压缩文件的操作快上许多。另外,压缩时不包含路径则添加命令参数"-ep",-ep:包含此开关时,文件在加入压缩文件时不会包含路径信息。这可能会有在压缩文件中,存在数个相同名称的结果。例子:从当前磁盘压缩全部的*.bas文件而不含路径。WinRARa-r-epbsources\*.bas以上参数说明均摘自winrar自带的说明文档,如果你需要其他功能,可以自行查看.

要先引入using Microsoft.Win32;
/// <summary>
/// 压缩
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
ArrayList arrFiles = new ArrayList();
//要打包两个文件和一个文件夹
arrFiles.Add(@"c:\abc.xls");
arrFiles.Add(@"d:\xyz.xls");
arrFiles.Add(@"e:\uvc");
//压缩文件存放路径
string savestr = @"c:\kugoo";
//压缩文件名
string name = "test.rar";
this.CompressFileForReportTable(arrFiles, savestr, name);
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourceFilesPaths">要压缩的文件路径列表</param>
/// <param name="compressFileSavePath">压缩文件存放路径</param>
/// <param name="compressFileName">压缩文件名(全名)</param>
p lic void CompressFileForReportTable(ArrayList sourceFilesPaths, string compressFileSavePath, string compressFileName)
{
String the_rar;
RegistryKey the_Reg;
System.Object the_Obj;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
//使用注册表对象获取到WiNRar路径
the_Reg = Registry.ClassesRoot.OpenS Key(@"Applications\WinRAR.exe\Shell\Open\Command");
the_Obj = the_Reg.GetVal("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.S string(1, the_rar.Length - 7);
the_Info = " a " + compressFileName;
foreach (object filePath in sourceFilesPaths)
{
the_Info += " " + filePath.ToString();//每个文件路径要与其它的空格隔开
}
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Normal;//Hidden 隐藏样式
the_StartInfo.WorkingDirectory = compressFileSavePath;//获取或设置要启动的进程的初始目录。(RAR存放路径)
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

/// <summary>
/// 解压缩指定的rar文件。
/// </summary>
/// <param name="rarFileToDecompress">rar文件(绝对路径)。</param>
/// <param name="directoryToSave">解压缩保存的目录。</param>
/// <param name="deleteRarFile">解压缩后删除rar文件。</param>
p lic void DecompressRAR(string rarFileToDecompress, string directoryToSave, bool deleteRarFile)
{
String the_rar;
RegistryKey the_Reg;
Object the_Obj;
the_Reg = Registry.ClassesRoot.OpenS Key(@"Applications\WinRAR.exe\Shell\Open\Command");
the_Obj = the_Reg.GetVal("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.S string(1, the_rar.Length - 7);

string winrarExe = the_rar;//需要在指定路径下放入winara.exe的可执行文件在安装目录下可以找到这个文件
if (new FileInfo(winrarExe).Exists)
{
//directoryToSave = CheckDirectoryName(directoryToSave);
try
{
Process p = new Process();
// 需要启动的程序名
p.StartInfo.FileName = winrarExe;
// 参数
string arguments = @"x -inul -y -o+";
arguments += " " + rarFileToDecompress + " " + directoryToSave;
p.StartInfo.Arguments = arguments;
p.Start();//启动
while (!p.HasExited)
{
}
p.WaitForExit();
}
catch (Exception ee)
{
throw new Exception("压缩文件在解压缩的过程中出现了错误!");
}
if (deleteRarFile)
{
File.Delete(rarFileToDecompress);
}
}
else
{
throw new Exception("本机上缺少必须的Winrar.exe文件,不能完成相应操作请联系管理员安装WinRar解压工具!");

"a -as"是压缩命令参数,这中间其实包含了2个命令参数
a:添加指定的文件和文件夹到压缩文件中
-as:同步压缩文件内容
如果此开关使用于压缩时,在当前添加的文件列表中不存在的被压缩文件,将会从压缩文件中删除。它可以很方便的与 -u 开关结合,用来同步压缩文件和压缩的文件夹的内容。
举例来说,在下面命令之后:
WinRAR a -u -as backup sources\*.cpp
压缩文件 backup.rar 将只会包含源文件夹的 *.cpp
文件,其它的全部文件将会从压缩文件中删除。它看起来就好象创建新压缩文件,但有一个重要的不同 :
如果从上次备份后没有修改过的文件,这项操作会比创建新压缩文件的操作快上许多。

另外,压缩时不包含路径则添加命令参数"-ep",
-ep:包含此开关时,文件在加入压缩文件时不会包含路径信息。这可能会有在压缩文件中,存在数个相同名称的结果。
例子:
从当前磁盘压缩全部的 *.bas 文件而不含路径。
WinRAR a -r -ep bsources \*.bas

以上参数说明均摘自winrar自带的说明文档,如果你需要其他功能,可以自行查看.


北流市18734816485: C#调用winrar压缩文件夹 -
木省肤痒: "a -as"是压缩命令参数,这中间其实包含了2个命令参数 a:添加指定的文件和文件夹到压缩文件中 -as:同步压缩文件内容 如果此开关使用于压缩时,在当前添加的文件列表中不存在的被压缩文件,将会从压缩文件中删除.它可以很方便的...

北流市18734816485: 如何用C# 调用WinRAR 解压RAR文件 -
木省肤痒: Process.Start("winrar.exe", "x \"F:\\vs\\debug.rar\" \"F:\\vs\"");

北流市18734816485: C# 如何读取rar压缩包里面的指定文件 -
木省肤痒: FileStream常用的属性和方法: 属性: CanRead 判断当前流是否支持读取,返回bool值,True表示可以读取 CanWrite 判断当前流是否支持写入,返回bool值,True表示可以写入 方法: Read() 从流中读取数据,返回字节数组 Write() 将字...

北流市18734816485: 用c# 怎么读取zip文件,调用rar.exe进行ZIP文件解压? -
木省肤痒: 你可以引用这个ddl文件ICSharpCode.SharpZipLib,下面是解压代码: public void UnZip(string zippath){if (!File.Exists(zippath))//如果文件没有找到,则报错{MessageBox.Show(string.Format("要解压的zip文件 {0} 不存在.", zippath), ...

北流市18734816485: C++怎么实现解压RAR或者ZIP文件 -
木省肤痒: 可以用system 去调用批处理的#include#include#include int main() { char rarPath[256]; puts(_T("填入你系统的WINRAR.exe所在目录:")); gets(rarPath); SetCurrentDirectory(rarPath);//解压c:\1.rar到Myjpg目录里 char* lcm="rar x \"c:\\1.rar\" \"c:\\Myjpg\\\""; printf("shell command line :%s\n",lcm); system(lcm); puts(_T("解压完毕.")); return 0; }

北流市18734816485: c#调用执行cmd命令 -
木省肤痒: 我这里写了一个利用winrar.exe解压的文件,是可以执行的,确保服务器上安装winrar.exe工具 你再仔细检查下你的方法,using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using ...

北流市18734816485: (C#)winform中实现选择一个文件夹,将其压缩复制到另外一个路径 ///楼主这个问题怎么解决的呢?
木省肤痒: using System.Diagnostics;Process process = new Process();process.StartInfo.FileName = @"C:\Program Files\WinRAR\Winrar.exe" //这是你安装Winrar的路径process.StartInfo.Argument = @" a -r C:\abc.rar C:\abc\drra"//第一个路径是压缩文件将要拷贝到的位置,第二个路径是你要压缩的文件夹process.Start();//这句话之后就开始压缩了

北流市18734816485: VC如何调用rar 压缩多个文件为一个文件 -
木省肤痒: system("C:/Progra~1/WinRAR/WinRAR.exe a -ibck C:/bak.rar C:/aa.txt C:/bb.txt");原理:WinRAR支持命令行操作.启动WinRAR并传入相应的压缩参数即可完成文件的压缩与解压.启动外部应用程序也可以使用WinExec,ShellExecute ,...

北流市18734816485: C#怎么使用RAR.exe的命令行 -
木省肤痒: 就是普通的启动其它程序 System.Diagnostics.Process p=new System.Diagnostics.Process(); p.StartInfo.FileName=@"d:\rar.exe";//需要启动的程序名 p.StartInfo.Arguments="a ed ed.doc";//启动参数 p.Start();//启动//if(p.HasExisted)//判断是否运行结束//p.kill();

北流市18734816485: C# 如何读取rar压缩包里面的文件信息 -
木省肤痒: 貌似C#只能读取ZIP格式的压缩文件,而且要读取必须解压的. 你直接通过WinRar打开这些压缩文件,解压其中需要的那个文件效果也是一样的.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网