Question

I have a procedure for making .rar file.

Code

    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. Can any one tell me how can i make rar of a complete folder with sub folders and files. Sorry 1 mistake and i also need selected extension files from given folder.

Was it helpful?

Solution 2

-r argument can recursive folder and files..

so you add "-r" to

 string cmdArgs = string.Format("A {0} {1} -ep -r",
        String.Format("\"{0}\"", rarPackagePath),
        fileList);

OTHER TIPS

Updated function

    /// <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. -r argument can recursive folder and files. Thanks to bystander. And also u can specify extension for packaging.

Ex.

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

Un-Rar

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

Ashish, Thanks for posting this; 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.

But I'm curious -- why is accFiles a dictionary, rather than a string[] or a Collection? Microsoft suggests not to pass dictionaries in public APIs: http://msdn.microsoft.com/en-us/library/dn169389(v=vs.110).aspx

Also, using a StringBuilder would clean up building fileList. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top