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);
        }
    }
有帮助吗?

解决方案

这是相对于 当前的工作目录, ,或绝对路径。这基本上是路径的标准过程。

编辑:您保存的路径与邮政编码中的目录无关。任何一个:

using(ZipFile f = new ZipFile("zip_dir/foo.zip"))
{
       f.AddFile("foo.txt");
       f.Save();
}

或者

using(ZipFile f = new ZipFile())
{
        f.AddFile("foo.txt");
        f.Save("zip_dir/foo.zip");
}

做正确的事情,即在./zip_dir/foo.zip中创建一个包含一个foo.txt文件的zip文件。当然,您可以在ZIP中创建子目录,但这是一个单独的问题。

其他提示

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

您可以通过使用第二个参数添加文件来指定文件存储在存档中。@"\" 表示所有文件都将存储在root中。

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

您正在使用的构造函数 ZipFile z = new ZipFile(...) 采用路径是保存zip文件的位置。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top