c# - 如何使用 7z 函数将每个 zip 文件从源文件夹放到目标文件夹

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

我有一个 window form,它包含两个按钮,让用户选择 input directoryoutput directory,如下所示。此外,我有一个 fileSystemWatcher 来监视空源文件夹和 timer 与 zip 功能一起使用。用户可以 select 一个目录(包含一些子文件夹)并单击 start 来创建一个 zip 文件,他们可以将该 zip 文件放入他们偏好的任何目录。

结果会是这样

但是,我未能使用 7zip 将 zip 文件创建到 selected 目录,命名与源文件夹中的子目录都不匹配。下面是我使用 7zip 处理 zip 函数的代码。

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

这是我的 7z 方法

public void CreateZipFile(string sourceName, string targetName)
{
    ProcessStartInfo zipProcess = new ProcessStartInfo();
    zipProcess.FileName = @"E:\Program Files-Zipz.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();
}

这是用户选择将zip文件放在哪个目录的按钮。

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

编辑:

您遇到的主要问题是选择目录而不是文件作为输出。

我制作了一个和你的类似的屏幕

选择输出和输入目录后

浏览按钮事件的代码:

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

}

和 zip 按钮事件的代码:

string zipProgramPath = @"C:\Program Files-Zipz.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);
            }
        }