-1

i'm working on the program to zip the zipped files in a folder with a password protected. For example i have multiple zipped file in a folder D:\Work\ZippedFile which is ABC.zip, CDE.zip, TZE.zip. I want to zip all the zipped file into one zip file named AllFile.zip with a password protected. What have i tried so far is as below:

string[] dirs = Directory.GetDirectories(@"D:\Work\ZippedFile");
foreach (string dir in dirs)
if (file.Contains(".zip"))
{
    using (var zip = new ZipFile())
    {
        string filename = file.Substring(51);
        zip.Password = "Abc!23";
        zip.Encryption = EncryptionAlgorithm.WinZipAes256;
        zip.AddFile(file, "");
        zip.Save(dir + "\\" + "AllFile.zip");
    }
}                    

I know this will output a single zip file for each zipped file, I hope somebody can give me an idea on how to achieve the right output. Thanks in advance.

Naura
  • 55
  • 1
  • 1
  • 7
  • 3
    isn't it obvious? move saving and creating new instance of ZipFile outside the loop (only AddFile should be in loop) – Selvin Dec 13 '19 at 10:09
  • Does this answer your question? [How to zip multiple files using only .net api in c#](https://stackoverflow.com/questions/1243929/how-to-zip-multiple-files-using-only-net-api-in-c-sharp) – Sinatr Dec 13 '19 at 10:25

1 Answers1

0

Don't create a new zip file for each existing zip file. Create a single new zip file and add all the existing zip files to it.

using (var zip = new ZipFile())
{
    string[] dirs = Directory.GetDirectories(@"D:\Work\ZippedFile");

    foreach (string dir in dirs)
    {
        if (file.Contains(".zip"))
        {   
            string filename = file.Substring(51);
            zip.AddFile(file, "");
        }
    }

    zip.Password = "Abc!23";
    zip.Encryption = EncryptionAlgorithm.WinZipAes256;
    zip.Save(dir + "\\" + "AllFile.zip");
} 
robbpriestley
  • 3,050
  • 2
  • 24
  • 36