我有一些压缩文件夹/文件的代码:
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();
}
}
正如你所看到的,我从列表框中取出文件夹。例如,我添加了一些文件夹来压缩到一个文件。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
嗯…我应该加上这一行:
compressor.PreserveDirectoryRoot = true;
这个选项在默认情况下是禁用的,在启用之后-问题神奇地解决了:)