简体   繁体   中英

How to create separate zip files for all the files with password within a directory using DotNetZip?

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\

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM