如何在压缩时删除父文件夹名称并仅使用子目录名称

时间:2009-08-13 07:13:15

标签: asp.net

filesToInclude列表包含以下数据:

 c:/temp/folder1/data.1
 c:/temp/folder1/subfolder/data.1
 c:/temp/folder1/subfolder2/data.1

以下DotNetZip代码存储以/ temp /开头的整个路径,但我喜欢只存储/ folder1 / without / temp /前缀。

using (ZipFile zip = new ZipFile())
    {
        foreach (var f in filesToInclude)
        {
            zip.AddFile(f);
        }

1 个答案:

答案 0 :(得分:1)

只需将目录部分指定为第二个参数即可。这意味着从完整文件名中查找目录,然后将前导部分剥离。

String baseDirectory = "c:/temp/";
using (ZipFile zip = new ZipFile())
{
    foreach (var f in filesToInclude)
    {
        String directory = Path.GetDirectoryName(f);
        // Consider adding error checking here to make sure that
        // directory really does start with baseDirectory!
        String relative = directory.Substring(baseDirectory.Length);
        zip.AddFile(f, relative);
    }
}