CodeGo.net>如何使用7z功能将每个zip文件从源文件夹到目标文件夹

时间:2018-12-04 13:06:10

标签: winforms textbox filepath 7zip filesystemwatcher

我有一个window form,其中包含两个按钮,以便用户选择input directoryoutput directory,如下所示。另外,我有一个fileSystemWatcher来监视空的源文件夹,还有一个timer与zip功能一起使用。用户可以选择一个目录(包含一些子文件夹),然后单击start创建一个zip文件,然后他们可以根据自己的喜好将该zip文件放置到任何目录中。

enter image description here

结果将是这样

enter image description here

但是,我无法使用7zip将zip文件创建到所选目录,命名与源文件夹的子目录都不匹配。下面是我使用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\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();
}

这是用户选择放置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;
        }
    }

1 个答案:

答案 0 :(得分:1)

编辑:

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

我制作了与您相似的屏幕

enter image description here

选择输出和输入目录后

enter image description here 浏览按钮事件的代码:

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