/// <summary>
/// 增加密码解密加密的压缩文件
/// </summary>
/// <param name="ZipFile"></param>
/// <param name="SavePath"></param>
/// <param name="password"></param>
public static void DecompressFile(string ZipFilePath, string SavePath, string password)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(ZipFilePath));
s.Password = password;
//创建文件夹
Directory.CreateDirectory(SavePath);
try
{
ZipEntry theEntry;
#region 循环读取压缩包里面的文件
while ((theEntry = s.GetNextEntry()) != null)
{
string fileName = Path.GetFileName(theEntry.Name);
if (fileName != String.Empty)
{
#region 在计算文件数目的时候同时创建
//在计算文件数目的时候同时创建
if (Path.GetDirectoryName(theEntry.Name) != "")
{
if (!Directory.Exists(SavePath + "\\" + Path.GetDirectoryName(theEntry.Name)))
{
Directory.CreateDirectory(SavePath + "\\" + Path.GetDirectoryName(theEntry.Name));
}
}
#endregion
//解压文件到指定的目录
FileStream streamWriter = File.Create(SavePath + "\\" + theEntry.Name);
try
{
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;
}
}
}
catch (ZipException ex)
{
throw new Exception(ex.Message);
}
finally
{
streamWriter.Close();
}
}
}
#endregion
}
catch (Exception zex)
{
throw new Exception(zex.Message);
}
finally
{
s.Close();
}
}
本文介绍了一种使用C#解压带密码保护的ZIP文件的方法。通过ZipInputStream类读取ZIP文件并设置密码,可以逐个提取文件内容到指定路径。文章详细展示了如何创建必要的文件夹结构,并处理文件解压过程中的异常。
886

被折叠的 条评论
为什么被折叠?



