简体   繁体   中英

DotNetZip in C# webapp - how to change the name of the zip folder structure

this is the code in my app

 Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=" + "filename.zip");
            using (ZipFile zip = new ZipFile())
            {
                zip.AddEntry(@"F:\My Pictures\2011\DSC04854.jpg", @"\");
                zip.AddEntry(@"F:\My Pictures\2011\DSC04854.jpg", @"\");
                zip.AddEntry(@"F:\My Pictures\2011\DSC04854.jpg", @"\");
                zip.Save(Response.OutputStream);
            }
            Response.Close();

I would like to create a zip file with a simplified folder structure (namely one folder) that gets written as output.

I have seen some answers to similar questions and I have tried many suggestions however I do not get the desired outcome. This code produces a zip file with the full folder structure.

Can anyone help?

You are using AddEntry, what you want is AddFile; the 2nd parameter to AddFile (when a string) is the internal directory.

I had the same problem but using zip.AddFiles(filePaths) still create full hierarchical zip file. I come cross another overload that simply solve this problem. just use: zip.AddFiles(filePaths,false,".") the second parameter is > preserve the hierarchical structure the third parameter is the structure you just want to use to produce zip file. using

"." means no nested folder allowed or simply zip on the current folder!

When adding the entries to the Zip, the second parameter could be left empty to place the files in the root of the folder.

zip.AddEntry(link_to_file.format, string.Empty);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM