Search code examples
c#zipdirectorydotnetzipionic-zip

Prevent DotNetZip to create extra folder


I have use the below snippet code to make zip folder by Ionic.zip:

string lastFolder = packageSpec.FolderPath.Split('\\')[packageSpec.FolderPath.Split('\\').Length - 1];
string zipRoot = packageSpec.FolderPath + "\\Zip" + lastFolder;
string fileName = zipRoot + "\\" + lastFolder + ".zip";
Logging.Log(LoggingMode.Prompt, "Spliting to zip part...");
if (!Directory.Exists(zipRoot))
     Directory.CreateDirectory(zipRoot);
ZipFile zip = new ZipFile();
zip.AddDirectory(packageSpec.FolderPath, zipRoot);
zip.MaxOutputSegmentSize = 200 * 1024 * 1024; // 200 MB segments
zip.Save(fileName);

it works fine to create multiple zip part but make unexpected nested folder as following desc below: if the variables are :

FolderPath = C:\MSR\Temp\Export_1

zipRoot = C:\MSR\Temp\Export_1\ZipExport_1

fileName= C:\MSR\Temp\Export_1\ZipExport_1\Export_1.zip

My source is like the image below:

enter image description here

1- is my source folder with it's staff to zip

2- is zip folder will contains 1 zip.AddDirectory(packageSpec.FolderPath, zipRoot);

But I am ending up with :

enter image description here

so these folders MSR->Temp->Export_1->ZipExport_1->ZipExport1 are extera which means Export_1.zip should have directly source folder not that nested extra folders .

does anybody knows how I can change that snippet code to do it?

thanks in advance.


Solution

  • I'm answering this based on the documentation link (look for AddDirectory with 2 parameters):

    using (ZipFile zip = new ZipFile())
    {
        // files in the filesystem like MyDocuments\ProjectX\File1.txt , will be stored in the zip archive as  backup\File1.txt
        zip.AddDirectory(@"MyDocuments\ProjectX", "backup");
    
        // files in the filesystem like MyMusic\Santana\OyeComoVa.mp3, will be stored in the zip archive as  tunes\Santana\OyeComoVa.mp3
        zip.AddDirectory("MyMusic", "tunes");
    
        // The Readme.txt file in the filesystem will be stored in the zip archive as documents\Readme.txt
        zip.AddDirectory("Readme.txt", "documents");
    
        zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G") ;
        zip.Save(ZipFileToCreate);
    }
    

    Which means, Your code should look like:

    using(ZipFile zip = new ZipFile())
    {
        zip.AddDirectory(packageSpec.FolderPath, lastFolder);
        zip.Save(fileName);
    }
    

    Result:

    Export_1.zip          (archive)
    |-> Export_1          (folder)
        |-> Files&Folders (data)
        |-> Files&Folders (data)
        |-> Files&Folders (data)
    

    NOTE:

    It is important to use using syntax, as You are not destroying the ZIP file in the end, which might cause some casualties to real run of the program (like memory leak). Please check this article on MSDN -> https://msdn.microsoft.com/en-gb/library/system.idisposable(v=vs.110).aspx?cs-lang=csharp

    EDIT:

    As I wasn't really sure what is expected result, I was unable to provide what You want. But if You want no folder, why are You passing 2nd parameter then? Solution should be:

    zip.AddDirectory(packageSpec.FolderPath);