压缩我的文件夹,但不要覆盖C#中的现有文件夹

时间:2013-10-10 09:23:57

标签: c# directory overwrite fastzip

我正在使用第三方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());
    }
}

1 个答案:

答案 0 :(得分:2)

您可以在压缩前始终手动测试文件是否存在:

if (!File.Exists(filename))
    fastZip.CreateZip(filename, @"C:\SourceDirectory", recurse, filter);
else
    //exception or message