fearofawhackplanet
fearofawhackplanet

Reputation: 53396

DotNetZip add files without creating folders

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

Upvotes: 36

Views: 33964

Answers (4)

Łukasz Wojtanowski
Łukasz Wojtanowski

Reputation: 149

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)

Upvotes: 0

Anish Ansalan
Anish Ansalan

Reputation: 1

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

Upvotes: -2

theawesomecoder61
theawesomecoder61

Reputation: 35

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.

Upvotes: -3

Fosco
Fosco

Reputation: 38526

How about just:

zip.AddFile(file,"");

or

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

Upvotes: 81

Related Questions