【问题标题】:unable to archive and unarchive files using 7zip in asp.net无法在 asp.net 中使用 7zip 归档和取消归档文件
【发布时间】: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.

标签: c# asp.net iis 7zip


【解决方案1】:

如果您设置了正确的 dll 路径,我认为使用 (Environment.Is64BitProcess) 而不是 Environment.Is64BitOperatingSystem) 将起作用。请确保该路径中存在 dll。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 2017-07-21
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多