如何删除使用.NET一个Zip文件的目录? [英] How can I delete a directory in a Zip file using .NET?

查看:444
本文介绍了如何删除使用.NET一个Zip文件的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何删除一个.zip的目录和所有在里面(最好使用DotNetZip)的文件?

How would I delete a directory in a .zip and all the files in it (preferably using DotNetZip)?

现在,我通过所有的文件运行在压缩,但它不工作:

Right now I'm running through all the files in the zip, but it doesn't work:

foreach (ZipEntry e in zip)
{
    //If the file is in the directory I want to delete
    if(e.FileName.Substring(0, 9) == "FolderName/")
    {
        zip.RemoveEntry(e.FileName);                          
    }
}



有没有更好的办法,如果不是,怎么会我做这项工作?

Is there a better way, if not, how would I make this work?

推荐答案

第一因子评分。不要循环用foreach,而从集合中移除元素。结果
我会尝试这种方式。

First tought. Don't loop with foreach while removing elements from the collection.
I will try in this way

for(int x = zip.Count -1; x >= 0; x--) 
{ 
    ZipEntry e = zip[x];
    if(e.FileName.Substring(0, 9) == "FolderName/") 
        zip.RemoveEntry(e.FileName);                           
} 



不过,看着zip文件类的方法,我注意到的方法:
SelectEntries返回一个ICollection的。所以,我认为这是可能做到:结果
编辑:使用重载版本SelectEntries(字符串,字符串)

However, looking at the methods of the ZipFile class, I noticed the method: SelectEntries that return a ICollection. So I think it's possible to do:
Use overloaded version SelectEntries(string,string)

var selection = zip1.SelectEntries("*.*", "FolderName");
for(x = selection.Count - 1; x >= 0; x--)
{
    ZipEntry e = selection[x];
    zip.RemoveEntry(e.FileName);                           
}

在zip文件取消对所有项目的环

removing the loop on all entries in the zipfile

这篇关于如何删除使用.NET一个Zip文件的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆