DotNetZip add files without creating folders

给你一囗甜甜゛ 提交于 2019-11-26 16:33:45

问题


using (ZipFile zip = new ZipFile())
{
    foreach(string file in Directory.GetFiles(folder))
    {
        zip.AddFile(file, Path.GetFileName(file));
    }
    zip.Save("test.zip"));
}

Each time I add a file, it's creating a new subfolder for it.

So I want to end up with:

test.zip
    -  myDoc.doc
    -  myPdf.pdf

but I'm ending up with:

test.zip
    -  myDoc.doc
        -  myDoc.doc
    -  myPdf.pdf
        -  myPdf.pdf

回答1:


How about just:

zip.AddFile(file,"");

or

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



回答2:


Becouse aproved answer was 4 years ago now a days is another way (more elegant) to do this, if you want compres all file in directory (code above look like it) you can use:

ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName, compressionLevel, includeBaseDirectory)

You are interest with last parameter (includeBaseDirectory) and passing false value.

More info you can find here: CreateFromDirectory(String, String, CompressionLevel, Boolean)




回答3:


zip.AddFile(file, "..\...\".ToString.Replace("..\...\", null))



回答4:


This is what I did and it worked.

zip.AddFile(file, "..\...\".ToString.Replace("..\...\", Nothing))

It sends the file back to 2 folders and replaces the .....\ with Nothing.



来源:https://stackoverflow.com/questions/4125607/dotnetzip-add-files-without-creating-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!