DotNetZip:将文件添加到动态创建的存档目录 [英] DotNetZip: Add Files to Dynamically Created Archive Directory

查看:165
本文介绍了DotNetZip:将文件添加到动态创建的存档目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法想象这是很难做到的,但我一直没能得到它的工作。我有一个文件类,只是存储的位置,目录,我要压缩的文件的名称。我拉拉链的磁盘上的文件存在,因此FileLocation是完整路径。 ZipFileDirectory不存在于磁盘上。如果我有我的文件列表,

I can't imagine this is hard to do, but I haven't been able to get it to work. I have a files class that just stores the location, directory, and name of the files I want to zip. The files I'm zipping exist on disk so the FileLocation is the full path. ZipFileDirectory doesn't exist on disk. If I have two items in my files list,

{ FileLocation = "path/file1.doc", ZipFileDirectory = @"\", FileName = "CustomName1.doc" },

{ FileLocation = "path/file2.doc", ZipFileDirectory = @"\NewDirectory", FileName = "CustomName2.doc" }

我希望看到MyCustomName1.doc根,以及包含MyCustomName2.doc一个文件夹中名为NewDirectory,但发生的事情是他们都在根使用此代码结束:

I would expect to see MyCustomName1.doc in the root, and a folder named NewDirectory containing MyCustomName2.doc, but what happens is they both end up in the root using this code:

using (var zip = new Ionic.Zip.ZipFile())
{
    foreach (var file in files)
    {
        zip.AddFile(file.FileLocation, file.ZipFileDirectory).FileName = file.FileName;
    }

    zip.Save(HttpContext.Current.Response.OutputStream);
}

如果我用这个:

zip.AddFiles(files.Select(o => o.FileLocation), false, "NewDirectory");



然后创建新的目录,并把所有的文件里面,符合市场预期,但后来我失去的能力,使用自定义命名用这种方法,而且还引入了更多的复杂性,第一个方法是完全处理。

Then it creates the new directory and puts all of the files inside, as expected, but then I lose the ability to use the custom naming with this method, and it also introduces more complexities that the first method would handle perfectly.

有没有一种方法可以让我得到的第一个方法(AddFile())来如我所料工作?

Is there a way I can get the first method (AddFile()) to work as I expect?

推荐答案

在进一步的检查,因为发布的注释几分钟前,我怀疑设置文件名正在擦除存档路径。

On further inspection, since posting a comment a few minutes ago, I suspect that setting FileName is erasing the archive path.

测试证实了这一点。

的名称设置为@NewDirectory\CustomName2.doc将解决这个问题。

Setting the name to @"NewDirectory\CustomName2.doc" will fix the problem.

您还可以使用@\NewDirectory\\ \\CustomName2.doc

You can also use @"\NewDirectory\CustomName2.doc"

这篇关于DotNetZip:将文件添加到动态创建的存档目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆