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

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


下面是我的结果,并提供了一些例子作为这个问题的答案

  • “ Shelling out”到7z.exe-这是最简单和最有效的方法,我可以确认 效果很好。作为 Workmad3提到,我只需要保证7z.exe 安装在所有目标计算机上,这是我可以保证的。
  • 内存压缩中的 Zip -这指的是在发送到客户端之前压缩 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 aka LZMA SDK -我想我还不够聪明,不知道如何使用这个(这就是为什么我发布在这里) ... ... 任何工作代码示例演示如何创建一个7zip 归档文件,可以通过常规的7zip 程序提取?
  • CodeProject C # (. NET) Interface for 7-Zip Archive DLL ——只支持从7zip 归档中提取... 我需要创建它们!
  • SharpZipLib -根据他们的 常见问题,SharpZipLib 不支持7zip。
130296 次浏览

EggCafe 7Zip cookie example This is an example (zipping cookie) with the DLL of 7Zip.

CodePlex Wrapper This is an open source project that warp zipping function of 7z.

7Zip SDK The official SDK for 7zip (C, C++, C#, Java) <---My suggestion

.Net zip library by SharpDevelop.net

CodeProject example with 7zip

SharpZipLib Many zipping

If you can guarantee the 7-zip app will be installed (and in the path) on all target machines, you can offload by calling the command line app 7z. Not the most elegant solution but it is the least work.

I used the sdk.

eg:

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();
}
}
}

SevenZipSharp is another solution. Creates 7-zip archives...

Here's a complete working example using the SevenZip SDK in C#.

It will write, and read, standard 7zip files as created by the Windows 7zip application.

PS. The previous example was never going to decompress because it never wrote the required property information to the start of the file.

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");
}
}
}

I use this code

                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();
 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) { }
}

Just Install 7 zip from source and pass the parameter to the method.

Thanks. Please like the answer.

These easiest way is to work with .zip files instead of .7z and use Dot Net Zip

When spinning off 7zip commands to shell there are other issues like user privileges, I had issue with 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

SharpCompress is in my opinion one of the smartest compression libraries out there. It supports LZMA (7-zip), is easy to use and under active development.

As it has LZMA streaming support already, at the time of writing it unfortunately only supports 7-zip archive reading. BUT archive writing is on their todo list (see readme). For future readers: Check to get the current status here: https://github.com/adamhathcock/sharpcompress/blob/master/FORMATS.md

Install the NuGet package called SevenZipSharp.Interop

Then:

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

Some additional test-info on @Orwellophile code using a 17.9MB textfile.
Using the property values in the code-example "as is" will have a HUGE negative impact on performance, it takes 14.16 sec.

Setting the properties to the following do the same job at 3.91 sec (i.a. the archive will have the same container info which is: you can extract and test the archive with 7zip but there are no filename information)

Native 7zip 2 sec.

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
};

I did another test using native 7zip and a 1,2GB SQL backup file (.bak)
7zip (maximum compression): 1 minute
LZMA SDK (@Orwellophile with above property-setting): 12:26 min :-(
Outputfile roughly same size.

So I guess I'll myself will use a solution based on the c/c++ engine, i.a. either call the 7zip executable from c# or use squid-box/SevenZipSharp, which is a wrapper around the 7zip c/c++ dll file, and seems to be the newest fork of SevenZipSharp. Haven't tested the wrapper, but I hope is perform just as the native 7zip. But hopefully it will give the possibility to compress stream also which you obvious cannot if you call the exe directly. Otherwise I guess there isn't mush advantage over calling the exe. The wrapper have some additional dependencies so it will not make your published project "cleaner".

By the way it seems the .Net Core team consider implementing LZMA in the system.io class in .Core ver. 5, that would be great!

(I know this is kind of a comment and not an answer but to be able to provide the code snippet it couldn't be a comment)

Here is code to create and extract 7zip (based on LZMA SDK - C#)

Note: 7z archives created with same code can be unarchived. As code uses managed LZMA using earlier version of LZMA SDK