Search code examples
c#.netarchive7zipsevenzipsharp

Adding a file to a specific path in a 7-zip archive using SevenZipSharp


I am trying to add a file stored on my local disk to a 7-zip archive using SevenZipSharp and the 7z.dll. This file should be added to some path \a\b\c\... inside the archive. However, the CompressFiles method of SevenZipSharp's SevenZipCompressor does not seem to provide an overload for which the destination can be set.

How do I add a file to a specific path in a 7-zip archive using SevenZipSharp?


Solution

  • You can create in a temp folder your desired folder structure and the use the following:

    SevenZipCompressor compressor = new SevenZipCompressor();
    compressor.PreserveDirectoryRoot = true;
    compressor.CompressionMode = CompressionMode.Create;
    compressor.CompressDirectory(@"C:\Test", "ppp.zip");
    

    In my example, I created many subfolders under "C:\Test", and as a result, I got the same folder structure in the zip file starting with root "\test..."