Click here to Skip to main content
15,911,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have multiple folders and that folders having many files. I need to ZIP all the files ZIP name should be folder name accordingly with a password.

EX: 1. OUTPUT(Folder)/NO1/FILES_1(100 files) this should be ZIP as "FILES_1"
2. OUTPUT(Folder)/NO1/FILES_2(100 files) this should be ZIP as "FILES_2"
3. OUTPUT(Folder)/NO1/FILES_3(100 files) this should be ZIP as "FILES_3"


Thanks in ADVANCE!!!

What I have tried:

if (textBox2.Text == "")
           {
               MessageBox.Show("select folder to be Zip","Alert");
               return;
           }

           SaveFileDialog saveFileDialog2 = new SaveFileDialog();


           saveFileDialog2.Filter = "zip files (*.zip)|*.zip|rar files (*.rar)|*.rar";
           saveFileDialog2.FilterIndex = 2;

           saveFileDialog2.RestoreDirectory = true;

           if (saveFileDialog2.ShowDialog() == DialogResult.OK)
           {
               //Zip directory including all files
               string filename = textBox1.Text;
               try
               {

                   using (var zip = new ZipFile())
                   {
                       zip.AddDirectory(textBox2.Text,"");
                       zip.Save(saveFileDialog2.FileName.ToString());
                   }
                   MessageBox.Show("Files are Zipped Successfully!", "Alert");
               }
               catch (Exception ex)
               {
                   MessageBox.Show("Error during Zip operation!", "Alert");
               }
               clear();
Posted
Updated 11-Mar-20 23:38pm
Comments
Maciej Los 12-Mar-20 3:47am    
And the problem is....
OriginalGriff 12-Mar-20 4:21am    
And?
What does that do that you didn't expect, or not do that you did?
What have you tried?
Where are you stuck?
What help do you need?

 
Share this answer
 
Comments
Maciej Los 12-Mar-20 5:45am    
I'm afraid that ZipFile class does not support a password functionality...
Richard MacCutchan 12-Mar-20 6:09am    
I know, but that appears to be the one that OP is using. And it seems to me that almost nobody actually looks at the documentation these days to check what features are available.
Maciej Los 12-Mar-20 6:18am    
Sad, but true.
Seems, your problem is how to list all subdirectories...

Check this out: Directory.GetDirectories Method (System.IO) | Microsoft Docs[^]

C#
string inputFolder = @"C:\FullPathHere\NO1\";
string[] dirs = Directory.GetDirectories(inputFolder);
foreach(string dir in dirs)
{
        //get the name of file by passing folder name and extension
        string sFileName = $"{dir}.zip"
        //create zip file from directory
        ZipFile.CreateFromDirectory(dir, sFileName);
}


In case, you want to create archive file with password, then you need to use class which supports password. See: c# - create zip file in .net with password - Stack Overflow[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900