如何使用 .NET 创建 7-Zip 存档

本文关键字:7-Zip 存档 创建 NET 何使用 | 更新日期: 2023-09-27 17:47:21

如何从 C# 控制台应用程序创建 7-Zip 存档?我需要能够使用常规的、广泛使用的 7-Zip 程序提取档案。


以下是我的结果,并提供示例作为此问题的答案

  • "掏出"到 7z.exe - 这是最简单、最有效的方法,我可以确认它运行良好。正如workmad3所提到的,我只需要保证在所有目标机器上都安装了7z.exe,这是我可以保证的。
  • 7Zip 内存压缩 - 这是指在发送到客户端之前"在内存中"压缩 cookie;这种方法似乎有点前途。包装器方法(包装 LZMA SDK)返回类型 byte[] 。当我将byte[]数组写入文件时,我无法使用 7-Zip (File.7z is not supported archive ) 提取它。
  • 7zSharp Wrapper(可在 CodePlex 上找到) - 它包装了 7z exe/LZMA SDK。我从我的应用程序中引用了该项目,它成功创建了一些存档文件,但我无法使用常规的 7-Zip 程序 ( File.7z is not supported archive ) 提取文件。
  • 7Zip SDK 又名 LZMA SDK - 我想我不够聪明,无法弄清楚如何使用它(这就是我在这里发布的原因)......是否有任何工作代码示例可以演示创建能够由常规 7zip 程序提取的 7zip 存档?
  • 用于 7-Zip 存档 DLL 的代码项目 C# (.NET) 接口 - 仅支持从 7zip 存档中提取...我需要创建它们!
  • SharpZipLib
  • - 根据他们的常见问题解答,SharpZipLib不支持7zip。

如何使用 .NET 创建 7-Zip 存档

蛋咖啡 7Zip 饼干示例 这是一个使用 7Zip DLL 的示例(压缩饼干)。

代码复合包装器这是一个开源项目,具有7z的扭曲压缩功能。

7Zip SDK 7zip官方SDK(C,C++,C#,Java)<---我的建议

.net zip 库按 SharpDevelop.net

使用 7zip 的代码项目示例

SharpZipLib 许多压缩

如果可以保证 7-zip 应用程序将安装在所有目标计算机上(并在路径中),则可以通过调用命令行应用程序 7z 来卸载。不是最优雅的解决方案,但它是最少的工作。

SevenZipSharp是另一种解决方案。创建 7 个压缩存档...

下面是在 C# 中使用 SevenZip SDK 的完整工作示例。

它将写入和读取由Windows 7zip应用程序创建的标准7zip文件。

附言。前面的示例永远不会解压缩,因为它从未将所需的属性信息写入文件的开头。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SevenZip.Compression.LZMA;
using System.IO;
using SevenZip;
namespace VHD_Director
{
    class My7Zip
    {
        public static void CompressFileLZMA(string inFile, string outFile)
        {
            Int32 dictionary = 1 << 23;
            Int32 posStateBits = 2;
            Int32 litContextBits = 3; // for normal files
            // UInt32 litContextBits = 0; // for 32-bit data
            Int32 litPosBits = 0;
            // UInt32 litPosBits = 2; // for 32-bit data
            Int32 algorithm = 2;
            Int32 numFastBytes = 128;
            string mf = "bt4";
            bool eos = true;
            bool stdInMode = false;

            CoderPropID[] propIDs =  {
                CoderPropID.DictionarySize,
                CoderPropID.PosStateBits,
                CoderPropID.LitContextBits,
                CoderPropID.LitPosBits,
                CoderPropID.Algorithm,
                CoderPropID.NumFastBytes,
                CoderPropID.MatchFinder,
                CoderPropID.EndMarker
            };
            object[] properties = {
                (Int32)(dictionary),
                (Int32)(posStateBits),
                (Int32)(litContextBits),
                (Int32)(litPosBits),
                (Int32)(algorithm),
                (Int32)(numFastBytes),
                mf,
                eos
            };
            using (FileStream inStream = new FileStream(inFile, FileMode.Open))
            {
                using (FileStream outStream = new FileStream(outFile, FileMode.Create))
                {
                    SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
                    encoder.SetCoderProperties(propIDs, properties);
                    encoder.WriteCoderProperties(outStream);
                    Int64 fileSize;
                    if (eos || stdInMode)
                        fileSize = -1;
                    else
                        fileSize = inStream.Length;
                    for (int i = 0; i < 8; i++)
                        outStream.WriteByte((Byte)(fileSize >> (8 * i)));
                    encoder.Code(inStream, outStream, -1, -1, null);
                }
            }
        }
        public static void DecompressFileLZMA(string inFile, string outFile)
        {
            using (FileStream input = new FileStream(inFile, FileMode.Open))
            {
                using (FileStream output = new FileStream(outFile, FileMode.Create))
                {
                    SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
                    byte[] properties = new byte[5];
                    if (input.Read(properties, 0, 5) != 5)
                        throw (new Exception("input .lzma is too short"));
                    decoder.SetDecoderProperties(properties);
                    long outSize = 0;
                    for (int i = 0; i < 8; i++)
                    {
                        int v = input.ReadByte();
                        if (v < 0)
                            throw (new Exception("Can't Read 1"));
                        outSize |= ((long)(byte)v) << (8 * i);
                    }
                    long compressedSize = input.Length - input.Position;
                    decoder.Code(input, output, compressedSize, outSize, null);
                }
            }
        }
        public static void Test()
        {
            CompressFileLZMA("DiscUtils.pdb", "DiscUtils.pdb.7z");
            DecompressFileLZMA("DiscUtils.pdb.7z", "DiscUtils.pdb2");
        }
    }
}

我使用了 sdk。

例如:

using SevenZip.Compression.LZMA;
private static void CompressFileLZMA(string inFile, string outFile)
{
   SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder();
   using (FileStream input = new FileStream(inFile, FileMode.Open))
   {
      using (FileStream output = new FileStream(outFile, FileMode.Create))
      {
          coder.Code(input, output, -1, -1, null);
          output.Flush();
      }
   }
}
 string zipfile = @"E:'Folderx'NPPES.zip";
 string folder = @"E:'TargetFolderx";
 ExtractFile(zipfile,folder);
public void ExtractFile(string source, string destination)
        {
            // If the directory doesn't exist, create it.
            if (!Directory.Exists(destination))
                Directory.CreateDirectory(destination);
            //string zPath = ConfigurationManager.AppSettings["FileExtactorEXE"];
          //  string zPath = Properties.Settings.Default.FileExtactorEXE; ;
            string zPath=@"C:'Program Files'7-Zip'7zG.exe";
            try
            {
                ProcessStartInfo pro = new ProcessStartInfo();
                pro.WindowStyle = ProcessWindowStyle.Hidden;
                pro.FileName = zPath;
                pro.Arguments = "x '"" + source + "'" -o" + destination;
                Process x = Process.Start(pro);
                x.WaitForExit();
            }
            catch (System.Exception Ex) { }
        }

只需从源代码安装 7 zip 并将参数传递给方法即可。

谢谢。请喜欢这个答案。

我使用这段代码

                string PZipPath = @"C:'Program Files'7-Zip'7z.exe";
                string sourceCompressDir = @"C:'Test";
                string targetCompressName = @"C:'Test'abc.zip";
                string CompressName = targetCompressName.Split('''').Last();
                string[] fileCompressList = Directory.GetFiles(sourceCompressDir, "*.*");
                    if (fileCompressList.Length == 0)
                    {
                        MessageBox.Show("No file in directory", "Important Message");
                        return;
                    }
                    string filetozip = null;
                    foreach (string filename in fileCompressList)
                    {
                        filetozip = filetozip + "'"" + filename + " ";
                    }
                    ProcessStartInfo pCompress = new ProcessStartInfo();
                    pCompress.FileName = PZipPath;
                    if (chkRequestPWD.Checked == true)
                    {
                        pCompress.Arguments = "a -tzip '"" + targetCompressName + "'" " + filetozip + " -mx=9" + " -p" + tbPassword.Text;
                    }
                    else
                    {
                        pCompress.Arguments = "a -tzip '"" + targetCompressName + "'" '"" + filetozip + "'" -mx=9";
                    }
                    pCompress.WindowStyle = ProcessWindowStyle.Hidden;
                    Process x = Process.Start(pCompress);
                    x.WaitForExit();

一些关于使用 17.9MB 文本文件@Orwellophile代码的其他测试信息。
"按原样"使用代码示例中的属性值将对性能产生巨大的负面影响,需要 14.16 秒

将属性设置为以下内容在 3.91 秒处执行相同的工作(即存档将具有相同的容器信息,即:您可以使用 7zip 提取和测试存档,但没有文件名信息)

原生 7zip 2 秒

CoderPropID[] propIDs =  {
  //CoderPropID.DictionarySize,
  //CoderPropID.PosStateBits,
  //CoderPropID.LitContextBits,
  //CoderPropID.LitPosBits,
  //CoderPropID.Algorithm,
  //CoderPropID.NumFastBytes,
  //CoderPropID.MatchFinder,
  CoderPropID.EndMarker
};
object[] properties = {
  //(Int32)(dictionary),
  //(Int32)(posStateBits),
  //(Int32)(litContextBits),
  //(Int32)(litPosBits),
  //(Int32)(algorithm),
  //(Int32)(numFastBytes),
  //mf,
  eos
};

我使用本机 7zip 和 1,2GB SQL 备份文件 (.bak)
进行了另一项测试7zip (最大压缩): 1 分钟
LZMA SDK(具有上述属性设置的@Orwellophile):12:26 分钟 :-(
输出文件大小大致相同。

所以我想我自己会使用基于 c/c++ 引擎的解决方案,即要么从 c# 调用 7zip 可执行文件,要么使用 squid-box/SevenZipSharp,它是 7zip c/c++ dll 文件的包装器,似乎是 SevenZipSharp 的最新分支。还没有测试过包装器,但我希望它的性能就像原生 7zip 一样。但希望它能提供压缩流的可能性,如果您直接调用 exe,您显然无法压缩流。否则,我想与打电话给exe相比没有优势。包装器有一些额外的依赖项,因此它不会使已发布的项目"更干净"。

顺便说一下,.Net Core 团队似乎考虑在 system.io 类中实现 LZMA。核心版本5,那就太好了!

(我知道这是一种评论而不是答案,但为了能够提供代码片段,它不可能是评论)

在我看来,

SharpCompress是最聪明的压缩库之一。它支持LZMA(7-zip),易于使用且正在积极开发中。

由于它已经具有 LZMA 流媒体支持,因此在撰写本文时,不幸的是它仅支持 7-zip 存档读取。但是存档写作在他们的待办事项列表中(请参阅自述文件)。对于未来的读者:在此处检查以获取当前状态:https://github.com/adamhathcock/sharpcompress/blob/master/FORMATS.md

安装名为 SevenZipSharp.Interop 的 NuGet 包

然后:

SevenZipBase.SetLibraryPath(@".'x86'7z.dll");
var compressor = new SevenZip.SevenZipCompressor();
var filesToCompress = Directory.GetFiles(@"D:'data'");
compressor.CompressFiles(@"C:'archive'abc.7z", filesToCompress);

这些最简单的方法是使用.zip文件而不是.7z并使用 Dot Net Zip

将 7zip 命令剥离到 shell 时,还有其他问题,例如用户权限,我在使用 SevenZipSharp 时遇到了问题。

Private Function CompressFile(filename As String) As Boolean
Using zip As New ZipFile()
    zip.AddFile(filename & ".txt", "")
    zip.Save(filename & ".zip")
End Using
Return File.Exists(filename & ".zip")
End Function

以下是创建和提取 7zip 的代码(基于 LZMA SDK - C#)

注意:使用相同代码创建的 7z 存档可以取消存档。作为代码使用托管 LZMA 使用早期版本的 LZMA SDK