开发者

Zipping files preserving the directory structure

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\Log开发者_运维知识库Files\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.


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:

if (fileExtension != null)
                {
                    zip.AddFiles(from f in sourceDir.GetFiles() where f.FullName.EndsWith(fileExtension) select f.FullName,false,"");

                }
                else
                {
                    zip.AddFiles(from f in sourceDir.GetFiles() select f.FullName,false,"");
                }
                zip.Save(DestinationDir + OutFileName);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜