2

我目前正在制作一个允许用户输入最多 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。

我希望我提供了足够的信息。

4

2 回答 2

0

好吧,你可以这样做:

var strings = Controls.OfType<TextBox>()
                      .Select(x => x.Text)
                      .Where(text => !string.IsNullOrEmpty(text))
                      .ToList();

using (ZipFile ZipIt = new ZipFile())
{
    nxtFileNum++;
    string comment = string.Format("This archive was created at {0:G}",
                                   DateTime.Now);
    foreach (string directory in strings)
    {
        ZipIt.AddDirectory(directory);
        ZipIt.Comment = comment;
        ZipIt.Save(Destination + "." + nxtFileNum);
    }
}   

这显然会拉出所有的文本框。另一种方法是使用类型或类似的集合,List<TextBox>而不是六个不同的变量。

请注意,即使用户没有指定前三个名称,它也会始终创建 .1、.2、.3 等。如果您想绝对忠实于用户给出的定位,请告诉我。

顺便说一句,我不清楚你是否真的应该重用同一个ZipFile对象。我希望这更合适:

string comment = string.Format("This archive was created at {0:G}",
                               DateTime.Now);
int fileIndex = 0;
foreach (string directory in strings)
{
    fileIndex++;
    using (ZipFile zipFile = new ZipFile())
    {
        zipFile.AddDirectory(directory);
        zipFile.Comment = comment;
        zipFile.Save(Destination + "." + fileIndex);
    }
}

(注意我是如何将变量重命名为更传统的,顺便说一下——变量通常以小写字母开头。)

于 2011-09-02T16:10:43.593 回答
0

你需要切换一些东西:

foreach (String q in BckupArray)
{
    nxtFileNum++;
    if (q != null)
    {
        using (ZipFile ZipIt = new ZipFile())
        {
            string Destination = @"C:\tmpZip" + nxtFileNum + ".zip";
            ZipIt.AddDirectory(q);
            ZipIt.Comment = "This archive was created at " + 
                            System.DateTime.Now.ToString("G");
            ZipIt.Save(Destination);
        }
    }
}     

原因:

  1. 该字符串Destination在您创建后是固定的。它不会改变,只是因为你增加了nxtFileNum.
  2. 你只创建了一个ZipFile,你只增加nxtFileNum了一次,因为那些在你的foreach循环之外
  3. 将创建 ZipFile 的部分放入if确保仅在真正使用实例时才创建实例。
于 2011-09-02T16:12:41.170 回答