[英]how to make rar file of full folder using c#
I have a procedure for making .rar file. 我有一个制作.rar文件的程序。
public static void RarFilesT(string rarPackagePath, Dictionary<int, string> accFiles)
{
string[] files = new string[accFiles.Count];
int i = 0;
foreach (var fList_item in accFiles)
{
files[i] = "\"" + fList_item.Value;
i++;
}
string fileList = string.Join("\" ", files);
fileList += "\"";
System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo();
string cmdArgs = string.Format("A {0} {1} -ep",
String.Format("\"{0}\"", rarPackagePath),
fileList);
sdp.ErrorDialog = true;
sdp.UseShellExecute = true;
sdp.Arguments = cmdArgs;
sdp.FileName = rarPath;//Winrar.exe path
sdp.CreateNoWindow = false;
sdp.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(sdp);
process.WaitForExit();
}
This producer needs an string array of file list for making rar file. 该生产者需要一个文件列表的字符串数组来制作rar文件。 Can any one tell me how can i make rar of a complete folder with sub folders and files.
谁能告诉我如何制作带有子文件夹和文件的完整文件夹的rar。 Sorry 1 mistake and i also need selected extension files from given folder.
对不起1错误,我还需要从给定文件夹中选择扩展文件。
/// <summary>
/// Package files. (Build Rar File)
/// </summary>
/// <param name="rarPackagePath">Rar File Path</param>
/// <param name="accFiles">List Of Files To be Package</param>
public static string RarFiles(string rarPackagePath,
Dictionary<int, string> accFiles)
{
string error = "";
try
{
string[] files = new string[accFiles.Count];
int i = 0;
foreach (var fList_item in accFiles)
{
files[i] = "\"" + fList_item.Value;
i++;
}
string fileList = string.Join("\" ", files);
fileList += "\"";
System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo();
string cmdArgs = string.Format("A {0} {1} -ep1 -r",
String.Format("\"{0}\"", rarPackagePath),
fileList);
sdp.ErrorDialog = false;
sdp.UseShellExecute = true;
sdp.Arguments = cmdArgs;
sdp.FileName = winrarPath;//Winrar.exe path
sdp.CreateNoWindow = false;
sdp.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(sdp);
process.WaitForExit();
error = "OK";
}
catch (Exception ex)
{
error = ex.Message;
}
return error;
}
For this u can make rar with full folder path. 为此,您可以使rar具有完整的文件夹路径。 -r argument can recursive folder and files.
-r参数可以递归文件夹和文件。 Thanks to bystander.
感谢旁观者。 And also u can specify extension for packaging.
您也可以指定包装扩展名。
string rarPackage = "E:/Backup.rar";
Dictionary<int, string> accFiles = new Dictionary<int, string>();
accFiles.Add(1, "D://*.txt");
accFiles.Add(2, "D://*.html");
accFiles.Add(3, "D://*.jpg");
RarFiles(rarPackage, accFiles);
public static void UnrarFiles(string rarPackagePath, string dir)
{
System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo();
string cmdArgs = string.Format("X {0} * {1}",
String.Format("\"{0}\"", rarPackagePath),
String.Format("\"{0}\"", dir));
sdp.Arguments = cmdArgs;
sdp.ErrorDialog = true;
sdp.UseShellExecute = true;
sdp.CreateNoWindow = false;
sdp.FileName = rarPath;
sdp.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(sdp);
process.WaitForExit();
}
-r argument can recursive folder and files.. -r参数可以递归文件夹和文件。
so you add "-r" to 所以你加“ -r”
string cmdArgs = string.Format("A {0} {1} -ep -r",
String.Format("\"{0}\"", rarPackagePath),
fileList);
Ashish, Thanks for posting this; Ashish,感谢您发布此信息; it's very helpful to me, as I've been told at the last moment that some files I have to FTP are supposed to be RARed first.
这对我非常有帮助,因为最后一刻我被告知必须先通过FTP传输的某些文件应该是RARed。
But I'm curious -- why is accFiles a dictionary, rather than a string[] or a Collection? 但是我很好奇-为什么accFiles是字典,而不是string []或Collection? Microsoft suggests not to pass dictionaries in public APIs: http://msdn.microsoft.com/en-us/library/dn169389(v=vs.110).aspx
Microsoft建议不要在公共API中传递字典: http : //msdn.microsoft.com/zh-cn/library/dn169389( v= vs.110).aspx
Also, using a StringBuilder would clean up building fileList. 另外,使用StringBuilder会清理构建文件列表。 I'd suggest this:
我建议这样做:
public static string RarFiles(string rarPackagePath,
Collection<string> accFiles)
{
string error = "";
try
{
StringBuilder fileListBuilder = new StringBuilder();
foreach (var fList_item in accFiles)
{
fileListBuilder.Append("\"" + fList_item + "\" ");
}
string fileList = fileListBuilder.ToString();
... (no change from this point on)
}
Again, thanks -- it's really been very helpful to me. 再次感谢您,这对我真的很有帮助。 I hope my suggestions are helpful to you, as well.
希望我的建议对您也有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.