1

我有一个制作.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();
    }

这个生产者需要一个文件列表的字符串数组来制作 rar 文件。谁能告诉我如何制作带有子文件夹和文件的完整文件夹的 rar。抱歉 1 个错误,我还需要从给定文件夹中选择的扩展文件。

4

3 回答 3

3

更新功能

    /// <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;
    }

为此,您可以使用完整的文件夹路径制作 rar。-r 参数可以递归文件夹和文件。感谢旁观者。您还可以指定包装的扩展名。

前任。

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();
    }
于 2013-07-12T10:22:59.830 回答
1

Ashish,感谢您发布此信息;这对我很有帮助,因为我在最后一刻被告知,我必须通过 FTP 传输的一些文件应该首先是 RARed。

但我很好奇——为什么 accFiles 是字典,而不是 string[] 或 Collection?微软建议不要在公共 API 中传递字典:http: //msdn.microsoft.com/en-us/library/dn169389 (v=vs.110).aspx

此外,使用 StringBuilder 会清理构建文件列表。我建议这样做:

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

再次感谢 - 这对我真的很有帮助。我希望我的建议对你也有帮助。

于 2014-10-24T14:24:29.477 回答
1

-r 参数可以递归文件夹和文件..

所以你添加“-r”到

 string cmdArgs = string.Format("A {0} {1} -ep -r",
        String.Format("\"{0}\"", rarPackagePath),
        fileList);
于 2013-04-13T14:36:11.690 回答