通过循环数组来归档每个字符串

发布于 2024-12-03 05:20:28 字数 1854 浏览 3 评论 0原文

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

我希望我给了大家足够的信息。

I am currently making a piece of software that will allow the user to enter up to 6 directories, each directory is saved as a string (within an array) the loop is then meant to check through the array and any that are not null i.e. actually have a directory assigned are meant to be zipped into their own archive. This is the code I have so far.

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

What I am trying to get this to do is save the first user given location to tmpZip0.7z, the second to tmpZip1.7z and so on however at the moment all it is doing is adding each directory to tmpZip0.zip.


Also as a side note, how would I get it to name each archive after the directory selected to be archived?

I am currently using DotNetZip (Ionic.Zip) dll.

I hope I gave enough information guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把梦留给海 2024-12-10 05:20:28

您需要切换一些内容:

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 确保只有在真正使用实例时才会创建实例。

You need to switch some stuff:

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

Reasons:

  1. The string Destination is fixed after you created it. It doesn't change, just because you increment nxtFileNum.
  2. You created only one ZipFile and you incremented nxtFileNum only once, because the those were outside of your foreach loop
  3. Putting the part that creates the ZipFile into the if makes sure an instance is only created if it is really used.
小镇女孩 2024-12-10 05:20:28

好吧,您可以这样做:

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 类型的集合或类似的集合来代替六个不同的变量。

请注意,即使用户没有指定前三个名称,始终会创建 .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);
    }
}

顺便说一下,请注意我如何将变量重命名为更常规 - 变量通常以小写字母开头。)

Well, you can do this with:

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

That will obviously pull all the textboxes though. An alternative is to have a collection of type List<TextBox> or something similar instead of the six different variables.

Note that that will always create .1, .2, .3 etc even if the user didn't specify the first three names. Let me know if you want to be absolutely faithful to the positioning the user gave.

It's not clear to me that you should really be reusing the same ZipFile object, by the way. I'd expect this to be more appropriate:

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

(Note how I've renamed the variables to be more conventional, by the way - variables typically start with a lower case letter.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文