Search code examples
c#sevenzipsharp

Set compression rootfolder in SevenZipSharp


I try to change an existing zipping tool. The zipping tool currently uses SharpZipLib. I have to convert it so that it uses SevenZipSharp. When zipping with SharpZipLib I can change the root directory of the zip and I want to keep this root directory when working with SevenZipSharp.

So if I have for example the absolute structure

C:/
├──root/
   └── folders/
       └── files/
           ├── text1.txt
           ├── text2.txt
           ├── text3.txt
           ├── text4.txt
           └── subfiles/
               └── text5.txt

Then my SevenSharpZip will create a zip with the content:

text1.txt
text2.txt
text3.txt
text4.txt
└── subfiles/
    └── text5.txt  

SharpZipLib will create the following

root/
└── folders/
    └── files/
        ├── text1.txt
        ├── text2.txt
        ├── text3.txt
        ├── text4.txt
        └── subfiles/
            └── text5.txt

Is it possible to set a root for the SevenZipSharp so that my two zips will have the same internal structure? I havent found a setting to change it accordingly. IncludeEmptyDirectory, PreserveDirectoryRoot and DirectoryStructure haven't got me the result I need.


Solution

  • I found the answer. To set a root folder I had to use the method

    public void CompressFiles(string archiveName, int commonRootLength, params string[] fileFullNames)
    

    There we have the commonRootLength. This is the starting point of our common root. For my example above this would mean, that the common root starts after the first "/" which means the commonRootLenght parameter needs to be 3.