【发布时间】:2018-06-18 07:31:46
【问题描述】:
我必须将文件从一台主机传输到另一台主机,所以我决定使用 zip (IONIC),但问题是它在处理更大的文件时会占用大量内存,所以我决定使用 7Z dll。我曾尝试实现压缩和解压缩,它在更改某些设置后在我的控制台上运行良好(未选中来自https://blog.jongallant.com/2011/10/7-zip-dll-file-does-not-exist/ 的优选 32 位)。
但在 ASP.net 应用程序上失败,当我尝试压缩或解压缩时,我收到“无法加载 7-zip 库或内部 COM 错误!消息:加载库失败。” .
我尝试过的代码。
protected void btnCompress_Click(object sender, EventArgs e)
{
var dllPath = "";
if (Environment.Is64BitOperatingSystem)
{
dllPath = HttpContext.Current.Server.MapPath(@"7z\7z64.dll");
}
else
dllPath = HttpContext.Current.Server.MapPath(@"7z\7z.dll");
var tmpPath = System.IO.Path.Combine(txtTempPath.Text, txtDir.Text);
try
{
SevenZip.SevenZipCompressor.SetLibraryPath(dllPath);
SevenZipCompressor sz = new SevenZipCompressor();
sz.CompressionLevel = CompressionLevel.Ultra;
sz.CompressionMode = CompressionMode.Create;
sz.CompressionMethod = CompressionMethod.Default;
sz.FastCompression = true;
sz.CompressDirectory(tmpPath, tmpPath + "_7Zip.7z");
//Directory.Delete(backupFolder, true);
Response.Write("<script>alert('Compressed successfully');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
//throw ex;
}
}
protected void btnExtract_Click(object sender, EventArgs e)
{
try
{
var tmpPath = System.IO.Path.Combine(txtTempPath.Text, txt7z.Text);
SevenZipExtractor.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll");
SevenZipExtractor zip = new SevenZipExtractor(tmpPath);
zip.ExtractArchive(System.IO.Path.GetFileNameWithoutExtension(tmpPath));
Response.Write("<script>alert('Extracted successfully');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
}
}
请有人为此提出适当的解决方案, 提前致谢。
【问题讨论】:
-
请确保您的 dllPath 正确。另外,查看这篇博文blog.jongallant.com/2011/10/7-zip-dll-file-does-not-exist
-
是的 dll 存在于正确的路径中。我在我的问题中提到了链接
-
调试代码时
if (Environment.Is64BitOperatingSystem)返回真还是假?我的意思是,如果它在一个环境(例如 64 位)上失败,请通过在调试时强制设置 dllPath 来尝试其他环境。如果它工作,则意味着代码没有根据环境加载正确的 dll -
我曾尝试使用 32 位和 64 位 dll 运行,但在这两种情况下都失败了
-
我相信你已经在你的项目中复制了 7z.dll 和 7z64.dll。您是否尝试过从
Program Files路径加载 7z.dll,例如C:\Program Files\7-Zip.