简体   繁体   English

SevenZipSharp-如何使用C#将多个目录压缩为单个文件?

[英]SevenZipSharp - how to compress multiple directories into a single file using c#?

I want to compress 3 folders into a single file using SevenZipCompressor . 我想使用SevenZipCompressor将3个文件夹压缩成一个文件。 I know how to compress a single folder . 我知道如何压缩单个文件夹。 Is such thing possible ?? 这样的事情可能吗?

Thank you ! 谢谢 !

The SevenZipCompressor class provides a method called CompressFileDictionary() . SevenZipCompressor类提供了一种称为CompressFileDictionary()的方法。 One of the method overloads expects a file dictionary and a file stream. 方法重载之一期望文件字典和文件流。 The file dictionary is a ordinary .Net Dictionary<string,string> . 文件字典是普通的.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. 词典的键使您可以控制7z存档中的结构。 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. 下面的示例仅显示如何自动为文件夹创建此类字典并将其压缩为单个7z存档文件的过程。

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: 您还可以使用以下代码更改来创建ZIP归档文件

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);                               
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM