C# 7z解压功能及问题

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

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

byte[] files=bomFile.filestream;
byte[] streamStandard = null;
List 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);
        }
        /// 
        /// 压缩文件
        /// 
        /// target.7z
        /// xxx.txt,xxx.txt
        /// 
        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;
        }
        /// 
        /// 加密压缩文件
        /// 
        /// target.7z
        /// xxx.txt,xxx.txt
        /// 
        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;
        }
        /// 
        /// 解压文件
        /// 
        /// 压缩的文件
        /// 解压的目标地址下
        /// 解密密码
        /// 
        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位勾选项。

C# 7z解压功能及问题_第1张图片

你可能感兴趣的:(.net,c#,开发语言)