为每个子目录创建一个 7zip 文件到 winforms 中输入路径的输出路径
Create a 7zip file for every subdirectory to a output path from input path in winforms
- 我有一个包含未知数量的输入路径
子目录。
- 我想用 7zip 压缩它们中的每一个,zip 文件将在选定的输出路径中。
下面是这个程序的概念。
下面是我尝试实现结果的 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-Zipz.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);
}
我记得曾经帮过你一次这个问题,我想我的回答并不令你满意,
这次我尝试得更好了:
这是 window:
这些是我使用的文件夹,就像在您的示例中一样:
'choose source' 和 'choose target' 按钮打开文件夹对话框
您的方向是正确的,一个遍历子目录的 for 循环。我想最困难的部分是获得正确的名字。您只需要确保目标名称具有“.7z”扩展名即可。
而且代码相当简单:
string zipProgramPath = @"C:\Program Files-Zipz.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);
}
}
}
所以在选择正确的目录并按下执行后
结果符合要求:
下面是 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;
.z a -t7z $outputfolder$compressFileName".7z" $folderPathToCompress
}
- 我有一个包含未知数量的输入路径
子目录。 - 我想用 7zip 压缩它们中的每一个,zip 文件将在选定的输出路径中。
下面是这个程序的概念。
下面是我尝试实现结果的 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-Zipz.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);
}
我记得曾经帮过你一次这个问题,我想我的回答并不令你满意, 这次我尝试得更好了:
这是 window:
这些是我使用的文件夹,就像在您的示例中一样:
您的方向是正确的,一个遍历子目录的 for 循环。我想最困难的部分是获得正确的名字。您只需要确保目标名称具有“.7z”扩展名即可。
而且代码相当简单:
string zipProgramPath = @"C:\Program Files-Zipz.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);
}
}
}
所以在选择正确的目录并按下执行后
结果符合要求:
下面是 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;
.z a -t7z $outputfolder$compressFileName".7z" $folderPathToCompress
}