Search code examples
winformstextboxfilepath7zipfilesystemwatcher

c# - How to put each zip file from source folder to target folder using 7z function


I have a window form, which contains two buttons to let a user choose the input directory and output directory like below. In addition, I have a fileSystemWatcher to monitor the empty source folder and timer to use with the zip function. The user can select a directory (which contain some sub-folder) and click start to create a zip file, and they can put that zip file to any directories from their preference.

enter image description here

the result will be like this

enter image description here

However, I failed to create the zip file to the selected directory using 7zip, neither the naming matches the subdirectory from the source folder. Below is my code to process the zip function using 7zip.

string source = textBoxInput.Text + "\\*";
string[] files = Directory.GetFiles(textBoxInput.Text, "*.7z", SearchOption.AllDirectories);
string target = tBoxOutput.Text + "\\everySingleZipFile"; // the target location only contains zip file from the source location

foreach (var file in files)
{
  // process zip for every file, no idea how to implement it.
  _sevenZip.CreateZipFile(source, target);
}

Here is my 7z method

public void CreateZipFile(string sourceName, string targetName)
{
    ProcessStartInfo zipProcess = new ProcessStartInfo();
    zipProcess.FileName = @"E:\Program Files\7-Zip\7z.exe"; // select the 7zip program to start
    zipProcess.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
    zipProcess.WindowStyle = ProcessWindowStyle.Minimized;
    Process zip = Process.Start(zipProcess);
    zip.WaitForExit();
}

This is the button for the user to choose which directory to put the zip file.

private void btnOutput_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.Description = $"Choose an output path";

        if (fbd.ShowDialog() == DialogResult.OK)
        {
            // show the path in the text box
            tBoxOutput.Text = fbd.SelectedPath;
        }
    }

Solution

  • EDIT:

    the main problem you have is choosing a directory as an output instead of a file.

    I made a screen similar to yours

    enter image description here

    after choosing directories for output and input

    enter image description here the code for the browse button events:

    private void btnBrowseInput_Click(object sender, EventArgs e)
    {
        using (var fbd = new FolderBrowserDialog())
        {
            DialogResult result = fbd.ShowDialog();
    
            if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
            {
                txtInput.Text = fbd.SelectedPath;
            }
        }
    
    }
    
    private void btnBrowseOutput_Click(object sender, EventArgs e)
    {
         if (string.IsNullOrEmpty(txtInput.Text))
            {
                MessageBox.Show("Please choose an input folder first");
                return;
            }
            using (var fbd = new FolderBrowserDialog())
            {
                DialogResult result = fbd.ShowDialog();
    
                if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
                {
                    var directoryName = Path.GetFileName(txtInput.Text);
                    txtOutput.Text = Path.Combine(fbd.SelectedPath, directoryName + ".7z");
                }
            }
    
    }
    

    and the code for the zip button event:

    string zipProgramPath = @"C:\Program Files\7-Zip\7z.exe";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnZip_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(txtInput.Text) || string.IsNullOrEmpty(txtOutput.Text))
                {
                    MessageBox.Show("Choose input directory and output file");
                }
                else
                {
                    CreateZipFile(txtInput.Text, txtOutput.Text);
                }
            }
            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);
                }
            }