压缩文件保留目录结构
我正在使用 codeplex 中的 DotNetZip DLL(http://dotnetzip.codeplex.com/) 来压缩程序中的一些文件。
我面临的问题是在压缩文件后,它保留了目录结构,当我提取 zip 文件时,所有父文件夹都会再次创建,然后只有我才能查看该文件。当源文件存在于中时,这非常烦人
,因此,如果我从 g:\Archive\LogFiles\W3SVC1\abc.log 压缩文件并在解压后创建“abc.zip”文件,文件夹 Archive\LogFiles\W3SVC1\正在创建,然后只有我才能看到 abc.log 文件。这里的“g:”是我的共享驱动器的名称。
我想删除所有这些父文件夹,以便我可以立即提取并找到压缩文件并打开它。我已检查压缩文件的 Path 属性,它显示 Archive\LogFiles\W3SVC1。不知何故,我需要以编程方式删除它,但无法轻松找到任何选项。
我使用的代码是这样的:
using (ZipFile zip = new ZipFile())
{
if (fileExtension != null)
{
zip.AddFiles(from f in sourceDir.GetFiles() where f.FullName.EndsWith(fileExtension) select f.FullName);
}
else
{
zip.AddFiles(from f in sourceDir.GetFiles() select f.FullName);
}
zip.Save(DestinationDir + OutFileName);
}
我还尝试了通过将reserveDirectoryHierarchy设置为“false”来尝试Addfiles的重载方法,但没有任何好处。
请让我知道该怎么做。
非常感谢。
I am using the DotNetZip DLL(http://dotnetzip.codeplex.com/) from codeplex to zip some files in my program.
Problem I am facing is after zipping the files it is preserving the directory structure and when I am extracting the zip file all the parent folders are getting created again and then only I am able to view the file. It is very annoying when the source file exists in
So, if I am zipping a file from g:\Archive\LogFiles\W3SVC1\abc.log and creating 'abc.zip' file after extracting it, folders Archive\LogFiles\W3SVC1\ are getting created and then only I am able to see abc.log file. Here 'g:' is the name of my shared drive.
I want to get rid of all these parent folders so that I can straight away extract and reach to the zipped file and open it. I have checked the Path property of the Zipped file and it is showing Archive\LogFiles\W3SVC1. Somehow I need to remove this programatically but not finding any option easily.
Code I am using is like this:
using (ZipFile zip = new ZipFile())
{
if (fileExtension != null)
{
zip.AddFiles(from f in sourceDir.GetFiles() where f.FullName.EndsWith(fileExtension) select f.FullName);
}
else
{
zip.AddFiles(from f in sourceDir.GetFiles() select f.FullName);
}
zip.Save(DestinationDir + OutFileName);
}
I have also tried the overload method of Addfiles by setting the reserveDirectoryHierarchy to 'false' but no benefit.
Please let me know what to do.
Many Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有正确调用 Addfiles 的重载方法。我传递了空字符串而不是 Null,现在它不保留目录结构。
所以更新后的代码如下:
I was not calling the overload method of Addfiles properly. instead of Null I passed empty string and now it is not preserving the directory structure.
so the updated code is like below: