c# - 什么是根目录或如何在 DotNetZip 中设置目录

标签 c# file-io dotnetzip

DotNetZip 从哪里获得它的根目录进行保存。所有保存示例均未显示目录。

我的目标是递归文件夹和子文件夹。在每个文件夹中,我想将所有文件压缩到一个 zip 中并删除源文件。

    private void CopyFolder(string srcPath, string dstPath)
    {
        if (!Directory.Exists(dstPath))
            Directory.CreateDirectory(dstPath);
        string[] files = Directory.GetFiles(srcPath);
        string msg;
        string zipFileName;
        using (ZipFile z = new ZipFile(Path.Combine(srcPath,String.Format("Archive{0:yyyyMMdd}.zip", DateTime.Now))))
        {
            z.ReadProgress += new EventHandler<ReadProgressEventArgs>(z_ReadProgress);
            foreach (string file in files)
            {
                FileInfo fi = new FileInfo(file);
                AddLog(String.Format("Adding {0}", file));
                z.AddFile(file);

            }
            //z.Save(Path.Combine(srcPath, String.Format("Archive{0:yyyyMMdd}.zip", DateTime.Now)));
            z.Save();
            if (deleteSource)
            {
                foreach (string file in files)
                {
                    File.Delete(file);
                }
            }

            zipFileName = z.Name;
        }
        if (!compressOnly)
            File.Copy(Path.Combine(srcPath,zipFileName), Path.Combine(dstPath, Path.GetFileName(zipFileName)));
        string[] folders = Directory.GetDirectories(sourcePath);
        foreach (string folder in folders)
        {
            string name = Path.GetFileName(folder);
            string dest = Path.Combine(dstPath, name);
            Console.WriteLine(ln);
            log.Add(ln);
            msg = String.Format("{3}{4}Start Copy: {0}{4}Directory: {1}{4}To: {2}", DateTime.Now.ToString("G"), name, dest, ln, Environment.NewLine);
            AddLog(msg);
            if (recurseFolders)
                CopyFolder(folder, dest);
            msg = String.Format("Copied Directory: {0}{4}To: {1}\nAt: {2}{3}", folder, dest, DateTime.Now.ToString("G"), Environment.NewLine);
            AddLog(msg);
        }
    }

最佳答案

有点晚了,也许我们在谈论两个不同的版本,但它可能仍然对其他人有帮助。

您可以使用 AddFile 的第二个参数指定文件在存档中的存储位置。 @"\" 表示所有文件将存储在根目录下。

z.AddFile(file,@"\");

关于c# - 什么是根目录或如何在 DotNetZip 中设置目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2675739/

相关文章:

c# - 从电子邮件字符串获取主机.net core

Java:将文本文件输出到控制台

ios - 在 iOS 中使用 NSFileManager 写入文件

Mac OS 中的 Java 文件锁定

c# - 使用 dotnetzip 在 zip 文件中创建虚拟目录树

c# - 有没有办法用另一个库解压缩 DynaZip Max 文件? F.E. DotNetZip

c# - 如何使用 Dotnetzip 库从 zip 中删除包含文件的特定文件夹?

c# - 回滚事务时出现异常 - 连接已关闭?

c# - 使用 MVVM 时创建 UserControl 的正确方法

c# - 与 "Reset to Match Style"等效的方法