1. 程式人生 > >7z.dll解壓縮

7z.dll解壓縮

res stat pen created dir zip sha .com ret

代碼中需引用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解壓文件夾);
}
}

7z.dll解壓縮