I have been archiving files using the "DotNetZip" library using code like this:
var files = new []
{
new { file = new FileInfo(@"C:\_test\Data\Testing\data.txt"), source = "Data" },
new { file = new FileInfo(@"C:\_test\Logs\Testing\data.txt"), source = "Logs" },
};
using (Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile())
{
zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
foreach (var f in files)
{
zipFile.AddFile(f.file.FullName, f.source);
}
zipFile.Save(@"C:\_test\output.zip");
}
The key thing to note here is that the file names are the same, they are directly contained within a folder with the same name, and when I create the Zip file I am creating logic folders for the files to sit in so there is no file name clash.
The problem is that the Zip compression is terrible. In fact I have examples of where I'm putting in a bunch of small files and getting a larger Zip!
I've then tested using 7-Zip and the results are excellent. The file sizes are much smaller than with Zip.
So, I've attempted to rewrite my code using 7-Zip, but there doesn't seem to be a good 7-Zip library for .NET and the only option is to use 7-Zip via the command line.
I've attempted this in Powershell:
."C:\Program Files\7-Zip\7z.exe" a -mx9 "C:\_test\20210112 Testing.7z" "C:\_test\Data\Testing\data.txt" "C:\_test\Logs\Testing\data.txt"
But I get this output:
7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21
Scanning the drive:
2 files, 40 bytes (1 KiB)
Creating archive: C:\_test\__test.7z
ERROR:
Duplicate filename on disk:
data.txt
data.txt
If I add the -spf
command-line then it works, but my archive is now created with every single folder starting from C
.
Is there a way in 7-Zip to allow it to create a logical folder structure when adding items from the command line?
To be clear, I'm selecting a subset of the files in the folders to be archived and I want to place them in logical folders.