7z.dll解压缩
代码中需引用SevenZipSharp.dll,项目路径下需存在7z.dll
public static string UnCompress7zZip(string str待解压文件, string str解压文件夹, string str密码=null)
{
String str7zRunPath = System.Windows.Forms.Application.StartupPath + @"\7z.dll";
if (!File.Exists(str7zRunPath))
{
return "7z.dll文件不存在!";
}
if (!File.Exists(str待解压文件))
{
return "待解压文件不存在!";
}
bool flag = false;
FileStream stream = new FileStream(str待解压文件, FileMode.Open, FileAccess.Read);
int num = stream.ReadByte();
stream.Close();
if ((num != 80) && (num != 0x52))
{
flag = true;
string destFileName = string.Format("{0}{1}{2}", Path.GetTempPath(), Guid.NewGuid().ToString(), ".zip");
Common_File.FileCopy(str待解压文件, destFileName, true);
Common_File.FileSetAttributes(destFileName, FileAttributes.Normal);
FileStream stream2 = new FileStream(destFileName, FileMode.Open);
stream2.WriteByte(80);
stream2.Close();
str待解压文件 = destFileName;
}
stream.Close();
SevenZip.SevenZipBase.SetLibraryPath(str7zRunPath);
try
{
SevenZip.SevenZipExtractor extractor;
if (string.IsNullOrEmpty(str密码))
{
extractor = new SevenZip.SevenZipExtractor(str待解压文件);
}
else
{
extractor = new SevenZip.SevenZipExtractor(str待解压文件, str密码);
}
if (!extractor.Check())
{
return "无效的压缩文件!";
}
if (!Directory.Exists(str解压文件夹))
{
Directory.CreateDirectory(str解压文件夹);
}
extractor.ExtractArchive(str解压文件夹);
extractor.Dispose();
if (flag)
{
HopeBid.Model.Common_File.FileDelete(str待解压文件);
}
return string.Empty;
}
catch (Exception exception)
{
return string.Format("解压缩异常:{0}" + Environment.NewLine + "待解压文件:{1}" + Environment.NewLine + "解压文件夹:{2}", exception.Message, str待解压文件, str解压文件夹);
}
}