0

I am zipping a directory in C# using ZipFile.CreateFromDirectory method:

private void createZIP()
{
    string startPath = @"c:\example\start";
    string zipPath = @"c:\example\result.zip";
    ZipFile.CreateFromDirectory(startPath, zipPath,  CompressionLevel.Fastest, true); 
}

It's working in general, but I just want the content of the /start/ folder in the zip file, not the directory itself.

Now:

result.zip
--start
---- file1.txt
---- file2.txt

How I want it:

result.zip
-- file1.txt
-- file2.txt

How can I achieve this?

knzo
  • 65
  • 1
  • 11

2 Answers2

7

The last parameter to ZIPFile.CreateFromDirectory, for which you are passing the value true, determines whether the directory itself should be included as the root of the ZIP. If you change this to false it should work as you desire.

ZipFile.CreateFromDirectory(startPath, zipPath,  CompressionLevel.Fastest, false); 
Adnan Umer
  • 3,669
  • 2
  • 18
  • 38
Ben Jackson
  • 1,108
  • 6
  • 9
  • Oh, ma bad. I skipped that part in the documentation. Thanks, it works like expected. – knzo Apr 22 '16 at 12:27
0

If you want to extend functionality beyond simple zipping to conditional zipping

use following link part[Adding or Removing Files from an Existing Zip File]

Creating-Zip-Files-Easily-in-NET

and to create empty directory use following link

creating-directories-in-a-ziparchive-c-sharp-net-4-5

Icarus
  • 415
  • 4
  • 12