C# 7z解压功能及问题

1、引用7z.dll(区分32位、64位)、Nuget程序包管理器中下载7z.Libs、SevenZipSharp。

2、本项目引用的是64位的7z.dll。dll下载链接

byte[] files=bomFile.filestream;
byte[] streamStandard = null;
List<byte[]> streamStandard_list;


Stream inStream = new MemoryStream(files);
R7z sevenz = new R7z();

SevenZipExtractor extractor = new SevenZip.SevenZipExtractor(inStream);
int numn = extractor.ArchiveFileData.Count;

 for (int index = 0; index < numn; index++)
{
//流式解压指定文件
  var outStream = new MemoryStream();
  extractor.ExtractFile(index, outStream);
  if (extractor.ArchiveFileData[index].FileName.Contains("standard"))
  {
      streamStandard = StreamToBytes(outStream);
      streamStandard_list.Add(streamStandard);
  }

}

 R7z.cs

using SevenZip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Oppein.Quotation.QuotationTraceService.ChaiBan.Models
{
    public class R7z
    {
        public R7z()
        {
            var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "7z\\7z.dll");
            SevenZipBase.SetLibraryPath(path);
        }
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="targetName">target.7z</param>
        /// <param name="files">xxx.txt,xxx.txt</param>
        /// <returns></returns>
        public bool CompressFiles(string targetName, params string[] files)
        {
            try
            {
                var tmp = new SevenZipCompressor();
                tmp.ScanOnlyWritable = true;
                tmp.CompressFiles(targetName, files);
                return true;
            }
            catch (Exception)
            { }
            return false;
        }
        /// <summary>
        /// 加密压缩文件
        /// </summary>
        /// <param name="targetName">target.7z</param>
        /// <param name="files">xxx.txt,xxx.txt</param>
        /// <returns></returns>
        public bool CompressFilesEncrypted(string targetName, string password, params string[] files)
        {
            try
            {
                var tmp = new SevenZipCompressor();
                tmp.ScanOnlyWritable = true;
                tmp.CompressFilesEncrypted(targetName, password, files);
                return true;
            }
            catch (Exception)
            { }
            return false;
        }
        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="CompressFile">压缩的文件</param>
        /// <param name="targetAddress">解压的目标地址下</param>
        /// <param name="password">解密密码</param>
        /// <returns></returns>
        public bool Decompression(string CompressFile, string targetAddress, string password = "")
        {
            try
            {
                if (string.IsNullOrWhiteSpace(password))
                {
                    using (var tmp = new SevenZipExtractor(CompressFile))
                    {
                        for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
                        {
                            tmp.ExtractFiles(targetAddress, tmp.ArchiveFileData[i].Index);
                        }
                    }
                }
                else
                {
                    using (var tmp = new SevenZipExtractor(CompressFile, password))
                    {
                        for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
                        {
                            tmp.ExtractFiles(targetAddress, tmp.ArchiveFileData[i].Index);
                        }
                    }
                }
            }
            catch (Exception)
            { }
            return false;
        }
    }
}

出现的问题

1、Can not load 7-zip library or internal COM error! Message: DLL file does not exist.

      说明引用的7z.dll32位、64位与当前项目不一致。需要替换dll文件

2、Invalid archive: open/read error!(7Z无效存档:打开/读取错误!)

解决方案:1)代码中加入int a = IntPtr.Size。判断 IntPtr.Size的值,如何 IntPtr.Size == 4的话,很显然是 32bit,如何 IntPtr.Size == 8说明当前是 64bit。

                 2)点击项目右键属性,去掉首选32位勾选项。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值