c# - 为winforms中输入路径到输出路径的每个子目录创建一个7zip文件

标签 c# winforms 7zip

  1. 我的输入路径包含未知数量的
    子目录。
  2. 我想使用 7zip 来压缩它们,并且 zip 文件将位于所选的输出路径中。

以下是该程序的概念。

enter image description here

下面是我尝试实现的7zip代码,但不知道该怎么做。

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

下面是我在该程序中使用的命令行中的 7z。

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

最佳答案

我记得曾经帮助过你这个问题,我想我的回答并不令你满意, 这次我尝试得更好了:

这是窗口:

enter image description here

这些是我使用的文件夹,就像您的示例中一样:

enter image description here “选择源”和“选择目标”按钮打开文件夹对话框

你的方向是正确的,一个遍历子目录的 for 循环。我想困难的部分是得到正确的名字。您只需要确保目标名称具有“.7z”扩展名。

代码相当简单:

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

选择正确的目录并按执行后

enter image description here

结果符合要求:

enter image description here

关于c# - 为winforms中输入路径到输出路径的每个子目录创建一个7zip文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53868695/

相关文章:

c# - 一条SQL汇总不同的 'group by'级别

c# - 使用 CDO 通过代理发送电子邮件

zip - 我怎么知道 7zip 是否使用了 AES256?

ubuntu - Linux : E_FAIL 上的 7zip 多部分存档

c# - 获取在 mongodb c# 中具有值的嵌套对象

c# - Microsoft.Windows.Controls 是否缺少程序集引用?

c# - 在 silverlight 中使用 while(true) 会很危险吗?

c# - 使用 Azure.Messaging.ServiceBus.Administration 创建 ServiceBus 队列时出错

c# - 如何将文本框中的每个字符串转换为字符串?

Windows CMD 文件/文件夹压缩使用 7z 或替代 .tar.gz