Search code examples
c#loopsdotnetzip

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


Solution

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