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

时间:2018-12-20 12:27:15

标签: 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);
}

2 个答案:

答案 0 :(得分:1)

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

这是窗口:

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

答案 1 :(得分:1)

这是PowerShell脚本将执行的操作。 SourceFolders将具有您的test,test1,test2文件夹。压缩文件将存储在C:\ DestinationFolder中。您只需从PowerShell命令提示符运行此脚本。

Import-Module Microsoft.PowerShell.Management

$sourcefolders = Get-ChildItem "C:\SourceFolders"
$outputfolder = "C:\DestinationFolder"

for ($i=0; $i -lt $sourcefolders.Count; $i++) {

$folderPathToCompress = $sourcefolders[$i].FullName 
$compressFileName = $sourcefolders[$i].Name

"Compressing folder ="+$folderPathToCompress;

.\7z a -t7z $outputfolder\$compressFileName".7z" $folderPathToCompress

}