使用SharpZipLib,解压的时候怎么只解压部分文件

c_- 2015-01-22 04:02:07
如题,怎么只解压zip包的部分文件,因为我需要这个方法去批量解压,解压出来的大部分文件又是用不到的

比如:test.zip里有文件夹a、b、c、d、e,我只想要解压a,或者说只解压a、d(不知道能不能完成?),怎么做?


class UnZipClass
{
/// <summary>
/// 解压缩一个 zip 文件。
/// </summary>
/// <param name="zipedFile">The ziped file.</param>
/// <param name="strDirectory">The STR directory.</param>
/// <param name="password">zip 文件的密码。</param>
/// <param name="overWrite">是否覆盖已存在的文件。</param>
public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
{

if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith("\\"))
strDirectory = strDirectory + "\\";

using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
{
s.Password = password;
ZipEntry theEntry;

while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;

if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\";

string fileName = Path.GetFileName(pathToZip);

//生成解压目录
Directory.CreateDirectory(strDirectory + directoryName);

if (fileName != "")
{
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);

if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
}

s.Close();
}
}
}
...全文
328 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sunu4 2015-02-05
  • 打赏
  • 举报
回复
除了遍历搜索,没有其他好的方法了?
c_- 2015-01-26
  • 打赏
  • 举报
回复
引用 3 楼 tcmakebest 的回复:
取得 theEntry 后, 判断 name 不是需要的就 continue; 如此简单
能不能详细讲解下
tcmakebest 2015-01-24
  • 打赏
  • 举报
回复
取得 theEntry 后, 判断 name 不是需要的就 continue; 如此简单
wind_cloud2011 2015-01-24
  • 打赏
  • 举报
回复
全解压后提供所需要的文件,删除其它文件,
chengxiaolong518 2015-01-22
  • 打赏
  • 举报
回复
解压后把不需要的文件夹给删除了就OK了。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧