I want to compress 3 folders into a single file using SevenZipCompressor . I know how to compress a single folder . Is such thing possible ??
Thank you !
The SevenZipCompressor
class provides a method called CompressFileDictionary()
.
One of the method overloads expects a file dictionary and a file stream.
The file dictionary is a ordinary .Net Dictionary<string,string>
.
The key of the dictionary is the name (or relative path) of the file in
the archive, the value of the dictionary is the path to the file in the file system.
The key of the dictionary allows you to control the structure in the 7z archive. For example if you want to compress the three folders
c:\temp\testdir1
|- file1.txt
|- file2.txt
c:\temp\testdir2
|- file1.txt
c:\temp2\test
|- file3.txt
and the resulting structure in the archive should be
testdir1
|- file1.txt
|- file2.txt
testdir2
|- file1.txt
test
|-file3.txt
then just add the files to the dictonary in the following way:
Dictionary<string, string> filesDic = new Dictionary<string, string>();
filesDic.Add(@"testdir1\file1.txt", @"c:\temp\testdir1\files1.txt");
filesDic.Add(@"testdir1\file2.txt", @"c:\temp\testdir1\files2.txt");
filesDic.Add(@"testdir2\file1.txt", @"c:\temp\testdir2\files1.txt");
filesDic.Add(@"test\file3.txt", @"c:\temp2\test\files3.txt");
The example below just shows how to automate the process of creating such a dictionary for folders and compress it into a single 7z archive file.
private static void AddFilesFromDirectoryToDictionary(Dictionary<string, string> filesDictionary,
string pathToDirectory)
{
DirectoryInfo dirInfo = new DirectoryInfo(pathToDirectory);
FileInfo[] fileInfos = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo fi in fileInfos)
{
filesDictionary.Add(fi.FullName.Replace(dirInfo.Parent.FullName + "\\", "").ToLower(),
fi.FullName);
}
}
static void Main(string[] args)
{
// Set path to 7z library.
SevenZipCompressor.SetLibraryPath("7z.dll");
using (FileStream fs = new FileStream("c:\\temp\\test.7z", FileMode.Create))
{
SevenZipCompressor szc = new SevenZipCompressor
{
CompressionMethod = CompressionMethod.Lzma,
CompressionLevel = CompressionLevel.Normal,
CompressionMode = CompressionMode.Create,
DirectoryStructure = true,
PreserveDirectoryRoot = false,
ArchiveFormat = OutArchiveFormat.SevenZip
};
Dictionary<string, string> filesDictionary = new Dictionary<string, string>();
AddFilesFromDirectoryToDictionary(filesDictionary, @"c:\temp\testdir1");
AddFilesFromDirectoryToDictionary(filesDictionary, @"c:\temp\testdir2");
AddFilesFromDirectoryToDictionary(filesDictionary, @"c:\temp2\test");
szc.CompressFileDictionary(filesDictionary, fs);
}
}
You can also create a ZIP-archive using the following code changes:
using (FileStream fs = new FileStream("c:\\temp\\test.zip", FileMode.Create))
{
SevenZipCompressor szc = new SevenZipCompressor
{
CompressionMethod = CompressionMethod.Deflate,
CompressionLevel = CompressionLevel.Normal,
CompressionMode = CompressionMode.Create,
DirectoryStructure = true,
PreserveDirectoryRoot = false,
ArchiveFormat = OutArchiveFormat.Zip
};
Dictionary<string, string> filesDictionary = new Dictionary<string, string>();
AddFilesFromDirectoryToDictionary(filesDictionary, @"c:\temp\testdir1");
AddFilesFromDirectoryToDictionary(filesDictionary, @"c:\temp\testdir2");
AddFilesFromDirectoryToDictionary(filesDictionary, @"c:\temp2\test");
szc.CompressFileDictionary(filesDictionary, fs);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With