C# - 如何使用 7-zip 库创建常规 ZIP 存档(即不是 .7z,而是 .zip)?
这是我在这里提出的第一个问题,请耐心等待。
我的目标只是用 C# 创建一个基本的 .zip 存档。我尝试使用 .NET 的内置 GZipStream
类并成功实现了这一点,但随后我遇到了一个问题,即我无法将文件命名为“usercode.zip”,而不会导致存档文件丢失扩大。由于限制,我无法让我的程序将这些文件创建为“usercode.trf.zip”,这是我发现在存档中完整保留文件名扩展名的唯一方法。
我尝试过使用许多其他压缩库,但我似乎无法让它们正常工作或按照我想要的方式工作。
我发现了 SevenZipHelper
库,它提供了一些漂亮的函数来使用 LZMA(或 7-zip)库来压缩文件。
我使用的代码如下所示:
//Take the BF file and zip it, using 7ZipHelper
BinaryReader bReader = new BinaryReader(File.Open(pFileName, FileMode.Open));
byte[] InBuf = new byte[Count];
bReader.Read(InBuf, 0, InBuf.Length);
Console.WriteLine("ZIP: read for buffer length:" + InBuf.Length.ToString());
byte[] OutBuf = SevenZip.Compression.LZMA.SevenZipHelper.Compress(InBuf);
FileStream BZipFile = new FileStream(pZipFileName, FileMode.OpenOrCreate, FileAccess.Write);
BZipFile.Seek(0, SeekOrigin.Begin);
BZipFile.Write(OutBuf, 0, OutBuf.Length);
BZipFile.Close();
这使用 7-zip 算法巧妙地创建了一个压缩文件。问题是我不能保证使用该程序的客户端能够访问 7-zip,因此该文件必须采用正常的 zip 算法。我已经浏览了帮助程序以及 7-zip 库,似乎可以使用该库使用正常的“ZIP”算法来压缩文件。我似乎不知道该怎么做。我注意到在几个地方有属性设置,但我找不到任何文档或谷歌搜索来告诉我在哪里设置它。
我意识到可能有更好的方法来做到这一点,而且我只是错过了一些东西,但我不能永远坐着为这样一个看似简单的任务而奋斗。任何帮助将不胜感激。
this is my first question on here, so bear with me.
What I'm aiming to do is just create a basic .zip archive in C#. I have tried using the built-in GZipStream
class of .NET and have managed to accomplish this, but then I have the problem that I cannot name the file "usercode.zip" without the archived file losing it's extension. Due to constraints I cannot make my program create these files as "usercode.trf.zip", which is the only way I've found of leaving the filename's extension intact inside the archive.
I've tried using a number of other zipping libraries and I can't seem to manage getting them working properly or the way I want it to.
I came upon the SevenZipHelper
library that provides some nifty functions to use the LZMA (or 7-zip) library to compress a file.
The code I'm using looks as follows:
//Take the BF file and zip it, using 7ZipHelper
BinaryReader bReader = new BinaryReader(File.Open(pFileName, FileMode.Open));
byte[] InBuf = new byte[Count];
bReader.Read(InBuf, 0, InBuf.Length);
Console.WriteLine("ZIP: read for buffer length:" + InBuf.Length.ToString());
byte[] OutBuf = SevenZip.Compression.LZMA.SevenZipHelper.Compress(InBuf);
FileStream BZipFile = new FileStream(pZipFileName, FileMode.OpenOrCreate, FileAccess.Write);
BZipFile.Seek(0, SeekOrigin.Begin);
BZipFile.Write(OutBuf, 0, OutBuf.Length);
BZipFile.Close();
This creates a compressed file neatly, using the 7-zip algorithm. Problem is I can't guarantee that the clients using this program will have access to 7-zip, so the file has to be in normal zip algorithm. I've gone through the helper- as well as the 7-zip libraries and it seems it is possible to use this library to compress a file with the normal "ZIP" algorithm. I just cannot seem to figure out how to do this. I've noticed properties settings in a few places, but I cannot find any documentation or googling to tell me where to set this.
I realize there's probably better ways to do this and that I'm just missing something, but I can't sit and struggle with such a supposedly easy task forever. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您愿意,可以看一下这个库,我以前使用过它,并且使用起来非常简单:dotnetzip
编辑(示例):
If you want you can take a look at this library, I've used it before and it's preaty simple to use : dotnetzip
EDIT(example):