SevenZipSharp - how to compress multiple directories into a single file using c#?
Asked Answered
D

1

6

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 !

Dugas answered 9/8, 2013 at 19:34 Comment(0)
H
5

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);                               
 }
Harts answered 10/8, 2013 at 15:4 Comment(3)
Seems this only works for .7z files and not .zip files? is this correct?Allargando
@MarkRedman: You can also create a .zip archive. I've updated my answer to show how to create a .zip archive. I've also fixed a minor bug in the AddFilesFromDirectoryToDictionary() function.Harts
Your answer is correct. However, doing so, doesn't retain the modification date of each file. making each file like it was created just now. That's even a bigger issue now.Dugas

© 2022 - 2024 — McMap. All rights reserved.