Search code examples
c#winforms7zip

Create a 7zip file for every subdirectory to a output path from input path in winforms


  1. I have an input path that contains an unknown number of
    subdirectories.
  2. I want to use 7zip to zip each of them and the zip file will be in the selected output path.

Below is the concept of this program.

enter image description here

Below is the 7zip code I try to achieve the result, but no idea how to do.

 string source = textBoxInput.Text + "\\*";                
 string target = Path.Combine(tBoxOutput.Text, source + DateTime.Now.ToString());

 foreach (var folder in Directory.GetDirectories(source))
 {
   _sevenZip.CreateZipFile(folder, target);
 }

Below is the 7z in command line I use to this program.

try
{
  ProcessStartInfo zipProcess = new ProcessStartInfo();
  zipProcess.FileName = @"E:\Program Files\7-Zip\7z.exe";
  zipProcess.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
        zipProcess.WindowStyle = ProcessWindowStyle.Minimized;
        Process zip = Process.Start(zipProcess);
        zip.WaitForExit();
}
catch (Exception err)
{
   Console.WriteLine(err.Message);
}

Solution

  • I remember helping you once with that question , i guess my answer was not to your satisfaction, I've tried better this time:

    this is the window:

    enter image description here

    these are the folders I used, just like in your example:

    enter image description here 'choose source' and 'choose target' button opens a folder dialog

    you were in the right direction, a for loop that runs over the subdirectories. i guess the hard part was getting the correct names. you just need to make sure that the target name would have a ".7z" extension.

    and the code is fairly simple:

    string zipProgramPath = @"C:\Program Files\7-Zip\7z.exe";
    
    public Form1()
    {
        InitializeComponent();
    }
    public void CreateZipFile(string sourceName, string targetName)
    {
        try
        {
            ProcessStartInfo zipProcess = new ProcessStartInfo();
            zipProcess.FileName = zipProgramPath; // select the 7zip program to start
            zipProcess.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
            zipProcess.WindowStyle = ProcessWindowStyle.Minimized;
            zipProcess.UseShellExecute = true;
            Process zip = Process.Start(zipProcess);
            zip.WaitForExit();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    
    private void btnBrowseSource_Click(object sender, EventArgs e)
    {
        using (var fbd = new FolderBrowserDialog())
        {
            DialogResult result = fbd.ShowDialog();
    
            if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
            {
                lblSource.Text = fbd.SelectedPath; //label next to the button
            }
        }
    
    }
    private void btnBrowseTarget_Click(object sender, EventArgs e)
    {
        using (var fbd = new FolderBrowserDialog())
        {
            DialogResult result = fbd.ShowDialog();
    
            if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
            {
                lblTarget.Text = fbd.SelectedPath.ToString(); //label next to the button
            }
        }
    }
    
    private void btnExecute_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(lblSource.Text) || string.IsNullOrEmpty(lblTarget.Text))
        {
            MessageBox.Show("Choose input directory and output directory");
        }
        else
        {
            foreach (var folder in Directory.GetDirectories(lblSource.Text))
            {
                string folderName= Path.GetFileName(folder);
                string targetName = Path.Combine(lblTarget.Text, folderName+ ".7z" );
                CreateZipFile(folder, targetName);
            }
        }
    }
    

    so after choosing the right directories, and pressing execute

    enter image description here

    the result is as required :

    enter image description here