开发者

Archiving each string using by looping through an array

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 S开发者_如何学Goelection 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.


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.


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜