内存压缩中的 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)提取这些文件。
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.
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)
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)