繁体   English   中英

如何使用DotNetZip为目录中的所有带有密码的文件创建单独的zip文件?

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

我有一个源文件夹,其中有多个文件。 我需要为该源文件夹中的所有文件创建带有密码的单独的zip文件。

我尝试了一下,但没有得到想要的结果。 请指导我。 这是我正在尝试的代码。

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);
}

这些是命令行参数,因为这是一个控制台应用程序

-p viveknuna -s D:\Source -d D:\Destination\

试试这个... http://dotnetzip.codeplex.com/

将此库添加到您的项目中...

样例代码

步骤1:获取目录的文件名和文件位置

步骤2:使用循环直到该目录中所有文件的完整

步骤2.1:进行以下工作

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

就是这样...如果您不希望这样做,那么请详细说明您的期望。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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