【问题标题】:c# - How to put each zip file from source folder to target folder using 7z functionc# - 如何使用 7z 函数将每个 zip 文件从源文件夹放入目标文件夹
【发布时间】:2018-12-04 13:06:09
【问题描述】:

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

结果会是这样的

但是,我未能使用 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;
        }
    }

【问题讨论】:

  • 当您说“输出”时,您的意思是提取?
  • 输出目录?那么输出文件名呢?因为 .7z 是文件而不是目录
  • @Patty_Putty 所以你有一个现有的 zip 文件并且你想移动它?或者您有一个现有文件夹,并且您想将其归档到特定位置?
  • 这只会添加一个简单的 for-each 循环并将输入文件夹从 c:/path/path/folder 编辑到 c:/path/path/folder/subfolder1 你可以很容易地得到它for-each 循环,看看这个:stackoverflow.com/a/10668520/9615185
  • 那么输出文本框有什么用呢?并且 zip 文件位于桌面上,因为这是您选择的。如果您使用我的代码,输出文件应该是您在 FolderBrowserDialog 中选择的任何位置。

标签: winforms textbox filepath 7zip filesystemwatcher


【解决方案1】:

编辑:

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

我做了一个和你类似的屏幕

选择输出和输入目录后

浏览按钮事件代码:

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

【讨论】:

  • 感谢您的努力,但我并不想提取它。只需根据用户偏好将其“存储”到其他文件夹即可。
  • 我很抱歉。假设输入目录有一些要压缩的子文件夹(带有文件),用户也可以选择直接在其他目录中创建该 zip 文件,而不是移动或复制到不同的位置。
  • 更新后的答案获取所有输入的目标文件夹和文件,并根据用户的需要将它们归档到新的目标。
  • 如果我的回答对您有帮助,请投票并标记答案,如果没有,请告诉我有什么问题
猜你喜欢
  • 1970-01-01
  • 2017-03-31
  • 2020-04-14
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多