我有一些压缩文件夹/文件的代码:
SevenZipCompressor compressor = new SevenZipCompressor();
foreach (string listBoxItem in listBoxToCompress.Items)
{
string choosenPath = listBoxItem;
int fileCount = Directory.GetFiles(listBoxItem).Length;
int folderCount = Directory.GetDirectories(listBoxItem).Length;
if (fileCount == 0 && folderCount == 0)
{
MessageBox.Show("folder " + listBoxItem + " is empty");
}
else
{
listBox1.Items.Add("========================================");
listBox1.Items.Add("Compressing. "+listBoxItem+" Wait...");
listBox1.Refresh();
if (File.Exists(zipFile) == true)
{
compressor.CompressionMode = CompressionMode.Append;
compressor.ArchiveFormat = OutArchiveFormat.Zip;
compressor.CompressDirectory(choosenPath, zipFile);
++done;
}
else
{
compressor.CompressionMode = CompressionMode.Create;
compressor.ArchiveFormat = OutArchiveFormat.Zip;
compressor.CompressDirectory(choosenPath, zipFile);
++done;
}
listBox1.Items.Add("Done.");
listBox1.Refresh();
}
}
正如您所看到的,我正在从列表框中获取文件夹。对于前者我正在添加一些文件夹压缩到一个file.zip所以我的列表框看起来像:
+c:/test
+d:/somefolder
完整结构是:
+c:/test
|--c:/test/testfile.exe
|--c:/test/testfolder
d:/somefolder
|--d:/somefile.exe
但是现在,在压缩之后,我的存档看起来像是:
+archive.zip
|--testfile.exe
|--testfolder
|--somefile.exe
如何创建此覆盖文件夹。我希望存档看起来像:
+archive.zip
|-test
|----testfile.exe
|----testfolder
|-somefolder
|----somefile.exe
答案 0 :(得分:1)
嗯......我应该添加这一行:
compressor.PreserveDirectoryRoot = true;
默认情况下禁用此选项,启用后问题神奇地解决了:)