DotNetZip add files without creating folders

31,480

How about just:

zip.AddFile(file,"");

or

zip.AddFile(file,@"\");
Share:
31,480
fearofawhackplanet
Author by

fearofawhackplanet

Updated on November 15, 2020

Comments

  • fearofawhackplanet
    fearofawhackplanet over 3 years
    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
    
  • fearofawhackplanet
    fearofawhackplanet over 13 years
    Thats makes it even worse. A filepath of for example Documents\Process\Practices\text.doc will create a subfolder in the .zip for each level in the path so you end up with test.zip -> Documents -> Process -> Practices -> text.doc.
  • Praveen
    Praveen almost 11 years
    @Fosco This works. Is there any complete documentation for this? I am using IonicZip.dll from nuGet.
  • Mou
    Mou about 9 years
    dotnet zip compression ratio not good. i zip a 152 KB doc file with dotnet zip and when zip was created then i was zip file size was 136KB. is there any tweak exist which create small size zip file. share the knowledge. thanks
  • Clay
    Clay over 8 years
    @Mou - doc files are now like .zip files, and they are already mostly compressed. There are still sections inside that aren't compressed - so that external programs can read out property/summary information, etc. - but generally, they're already pretty compact.
  • Avtandil Kavrelishvili
    Avtandil Kavrelishvili about 6 years
    @Fosco thank you for great solution. You save my time.