0

I have a source folder in which multiple files are there. I need to create separate zip files with password for all the files within this source folder.

I tried it, but not getting the desired result. Please guide me. this is the code which I'm trying.

int codePage = 0;
ZipEntry e = null;
string entryComment = null;
string entryDirectoryPathInArchive = "";
string strFullFilePath = "";
using (ZipFile zip = new ZipFile())
{              
    zip.StatusMessageTextWriter = System.Console.Out;
    zip.UseUnicodeAsNecessary = true;
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    DateTime Timestamp = DateTime.Now;
    for (int i = 0; i < args.Length; i++)
    {
        switch (args[i])
        {
            case "-p":
                i++;
                if (args.Length <= i) Usage();
                zip.Password = (args[i] == "") ? null : args[i];
                break;
            case "-d":
                i++;
                if (args.Length <= i) Usage();
                string entryName = args[i];

                if (!System.IO.Directory.Exists(args[i]))
                {
                    System.IO.Directory.CreateDirectory(args[i]);
                }
                strFullFilePath = Path.Combine(args[i], ".zip");   
                break;
            case "-s":
                i++;
                if (args.Length <= i) Usage();
                string[] files = Directory.GetFiles(args[i]);
                // add all those files to the ProjectX folder in the zip file
                foreach (string file in files)
                {
                    zip.AddFile(file, "");
                }
                break;
            default:
                if (entryComment != null)
                {
                    // can only add a comment if the thing just added was a file. 
                    if (zip.EntryFileNames.Contains(args[i]))
                    {
                        e = zip[args[i]];
                        e.Comment = entryComment;
                    }
                    else
                        Console.WriteLine("Warning: ZipWithEncryption.exe: ignoring comment; cannot add a comment to a directory.");
                    // reset the comment
                    entryComment = null;
                }
                break;
        }
    }
    zip.Save(strFullFilePath);
}

These are the command line arguments, as this is a console application

-p viveknuna -s D:\Source -d D:\Destination\
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
user3264676
  • 253
  • 5
  • 8
  • 20
  • Please explain what you expect to happen and what results you actually get. – CodeCaster Feb 04 '14 at 12:44
  • I need to create separate zip files with password for all the files within this source folder. But I'm getting a single zip folder and inside that folder I am getting all the extracted files – user3264676 Feb 04 '14 at 13:25
  • Your problem is in the 'case "-s"' code. Search the site for something like "list files and subdirectories recursively", as you're currently only finding the files in the parent directory. – CodeCaster Feb 04 '14 at 22:59

1 Answers1

0

Try this one... http://dotnetzip.codeplex.com/

Add this library to your project...

Sample code

step 1: get file name and file location of directory

step 2: use loop untill complete of all files in that directory

step 2.1 : do the following work

       ZipFile zip = new ZipFile("filename"+".zip");
       zip.Password("Secret1!");
       zip.AddFile("file_location"+"filename_with_extenion");
       zip.Save();

that's it... if you are not expecting this out put, then explain what you are expecting in detail.

vino20
  • 429
  • 4
  • 13
  • I'm sorry but "use another library" is not the answer to "how do I zip a directory recursively", nor does your code demonstrate how to do that. – CodeCaster Feb 04 '14 at 23:00
  • @CodeCater, It just a piece of code to convert collection of file to a zip file that's it, then what is your problem man? – vino20 Feb 05 '14 at 04:38
  • It's a piece of code that zips one file, hardcoded, using a different.library than OP uses. OP is asking for code that, using SharpZipLib, zips a directory, recursively. My problem is you don't answer OP's question. :-) – CodeCaster Feb 05 '14 at 07:34