Tôi muốn nén 3 thư mục vào một tệp đơn bằng cách sử dụng SevenZipCompressor. Tôi biết cách nén một thư mục. Điều đó có thể xảy ra không ??SevenZipSharp - làm cách nào để nén nhiều thư mục vào một tệp đơn bằng C#?
Cảm ơn bạn!
Tôi muốn nén 3 thư mục vào một tệp đơn bằng cách sử dụng SevenZipCompressor. Tôi biết cách nén một thư mục. Điều đó có thể xảy ra không ??SevenZipSharp - làm cách nào để nén nhiều thư mục vào một tệp đơn bằng C#?
Cảm ơn bạn!
Lớp SevenZipCompressor
cung cấp phương thức được gọi là CompressFileDictionary()
. Một trong các quá tải phương thức mong đợi một từ điển tệp và luồng tệp. Từ điển tệp là thông thường .Net Dictionary<string,string>
. Chìa khóa của từ điển là tên (hoặc đường dẫn tương đối) của tệp trong lưu trữ, giá trị của từ điển là đường dẫn đến tệp trong hệ thống tệp.
Chìa khóa của từ điển cho phép bạn kiểm soát cấu trúc trong kho lưu trữ 7z. Ví dụ, nếu bạn muốn nén ba thư mục
c:\temp\testdir1
|- file1.txt
|- file2.txt
c:\temp\testdir2
|- file1.txt
c:\temp2\test
|- file3.txt
và cấu trúc dẫn đến việc lưu trữ nên
testdir1
|- file1.txt
|- file2.txt
testdir2
|- file1.txt
test
|-file3.txt
sau đó chỉ cần thêm các tập tin vào dictonary theo cách sau:
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");
Ví dụ dưới đây chỉ cho biết cách tự động hóa quá trình tạo từ điển như vậy cho các thư mục và nén nó vào một tệp lưu trữ 7z duy nhất.
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);
}
}
Bạn cũng có thể tạo ra một ZIP-lưu trữ sử dụng những thay đổi mã sau:
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);
}
@subirshan: Liệu câu trả lời giúp đỡ của tôi? – Hans
Dường như tính năng này chỉ hoạt động đối với các tệp .7z chứ không phải tệp .zip? điều này có đúng không? –
@MarkRedman: Bạn cũng có thể tạo một tệp nén .zip. Tôi đã cập nhật câu trả lời của mình để hiển thị cách tạo tệp nén .zip. Tôi cũng đã sửa một lỗi nhỏ trong hàm AddFilesFromDirectoryToDictionary(). – Hans