【问题标题】:How to create separate zip files for all the files with password within a directory using DotNetZip?如何使用 DotNetZip 为目录中的所有带密码的文件创建单独的 zip 文件?
【发布时间】:2014-02-05 12:45:10
【问题描述】:

我有一个源文件夹,其中有多个文件。我需要为此源文件夹中的所有文件创建带有密码的单独 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\

【问题讨论】:

  • 请说明您预期会发生什么以及您实际得到的结果。
  • 我需要为此源文件夹中的所有文件创建带有密码的单独 zip 文件。但是我得到了一个 zip 文件夹,在该文件夹中我得到了所有提取的文件
  • 您的问题出在'case "-s"' 代码中。在网站上搜索“递归列出文件和子目录”之类的内容,因为您目前只能在父目录中找到文件。

标签: c# dotnetzip


【解决方案1】:

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

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

示例代码

第一步:获取目录的文件名和文件位置

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

步骤 2.1:做以下工作

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

就是这样...如果您不期待这样的结果,请详细解释您的期待。

【讨论】:

  • 很抱歉,“使用另一个库”不是“如何递归压缩目录”的答案,您的代码也没有演示如何做到这一点。
  • @CodeCater,这只是将文件集合转换为 zip 文件的一段代码,那么你的问题是什么?
  • 这是一段代码,它使用与 OP 不同的.library 压缩一个硬编码的文件。 OP 要求使用 SharpZipLib 递归压缩目录的代码。我的问题是你不回答OP的问题。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
相关资源
最近更新 更多