压缩我的文件夹,但不要覆盖 C# 中的现有文件夹
本文关键字:文件夹 覆盖 我的 压缩 | 更新日期: 2023-09-27 18:34:54
我正在使用第三方 FastZip 来压缩我的文件夹,当我使用已经存在的文件压缩新文件夹时,比如说 abc.zip,Fast Zip 会覆盖这个旧的 abc.zip,删除旧文件并仅压缩新文件。
任何人都知道解决方案。
编辑-> 我设法自己做到了,如果有人需要,这里有一个解决方案。
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
string zipfilename = "abc.zip"; // Your required zip file
string fdrname = "abc";
if (!File.Exists(zipfilename)) // If file zip does not exists
{
Directory.CreateDirectory(fdrname); // Create folder with same name
FastZip fz = new FastZip(); // using FastZip dll
fz.CreateZip(zipfilename, fdrname, true, null); // create zip file (ofcourse its empty)
}
if (Directory.Exists(fdrname)) // delete folder which you have created (optional)
Directory.Delete(fdrname);
try
{
ZipFile zip = new ZipFile(zipfilename); // by Using ZipFile dll
zip.BeginUpdate();
zip.Add(pathtofile); // add file path which you want to zip in abc.zip
zip.CommitUpdate();
zip.Close();
}
catch (Exception e)
{
Console.WriteLine((e.ToString());
}
}
您始终可以在压缩之前手动测试文件是否存在:
if (!File.Exists(filename))
fastZip.CreateZip(filename, @"C:'SourceDirectory", recurse, filter);
else
//exception or message