我目前正在制作一个允许用户输入最多 6 个目录的软件,每个目录都保存为一个字符串(在一个数组中),然后循环用于检查数组以及任何不为空的内容,即实际上分配目录意味着将其压缩到自己的存档中。这是我到目前为止的代码。
private void ZipIt()
{
int nxtFileNum = 0;
string Destination = @"C:\tmpZip" + nxtFileNum + ".zip";
// Check all fields, check if empty, if not save to Selection array
// Seems a inefficient - Possibly loop through Text box control type and collect?
if (String.IsNullOrEmpty(tboxSelect1.Text) == false) { BckupArray[0] = tboxSelect1.Text; };
if (String.IsNullOrEmpty(tboxSelect2.Text) == false) { BckupArray[1] = tboxSelect2.Text; };
if (String.IsNullOrEmpty(tboxSelect3.Text) == false) { BckupArray[2] = tboxSelect3.Text; };
if (String.IsNullOrEmpty(tboxSelect4.Text) == false) { BckupArray[3] = tboxSelect4.Text; };
if (String.IsNullOrEmpty(tboxSelect5.Text) == false) { BckupArray[4] = tboxSelect5.Text; };
if (String.IsNullOrEmpty(tboxSelect6.Text) == false) { BckupArray[5] = tboxSelect6.Text; };
// Create a new ZipFile entity and then loop through each array member, checking if
// it has an assigned value, if so compress it, if not, skip it.
using (ZipFile ZipIt = new ZipFile())
{
nxtFileNum++;
foreach (String q in BckupArray)
{
if (q != null)
{
ZipIt.AddDirectory(q);
ZipIt.Comment = "This archive was created at " + System.DateTime.Now.ToString("G");
ZipIt.Save(Destination);
}
}
}
}
我要做的是将给定位置的第一个用户保存到 tmpZip0.7z,将第二个用户保存到 tmpZip1.7z 等等,但目前它所做的只是将每个目录添加到 tmpZip0.zip。
另外作为旁注,我将如何在选择要存档的目录之后命名每个存档?
我目前正在使用 DotNetZip (Ionic.Zip) dll。
我希望我提供了足够的信息。