C # - How to create a regular ZIP archive using a 7-zip library (i.e. Not .7z, but .zip)?

This is my first question here, so bear with me.

What I'm going to do is just create a basic .zip archive in C #. I tried using the built-in GZipStream .NET class and was able to accomplish this, but then I have a problem that I cannot name the file "usercode.zip" without losing its extension. Due to limitations, I cannot force my program to create these files as "usercode.trf.zip", which is the only way I found to leave the file extension unchanged inside the archive.

I tried using a number of other zipping libraries, and I cannot get them to work properly or the way I want.

I came across the SevenZipHelper library, which provides some useful functions for using the LZMA (or 7-zip) library to compress a file.

The code I use is 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 the compressed file neatly using the 7-zip algorithm. The problem is that I can not guarantee that clients using this program will have access to 7-zip, so the file should be in the usual zip algorithm. I went through the assistant, as well as the 7-zip libraries, and it looks like this library can be used to compress the file using the usual "ZIP" algorithm. I just can't figure out how to do this. I noticed property settings in several places, but I can not find the documentation or google search to tell me where to set it.

I understand that there are probably better ways to do this, and that I just missed something, but I can’t sit and fight such a simple task forever. Any help would be greatly appreciated.

+4
source share
1 answer

If you want, you can take a look at this library, I used it before and its easy to use: dotnetzip

EDIT (example):

  using (ZipFile zip = new ZipFile()) { foreach (String filename in FilesList) { Console.WriteLine("Adding {0}...", filename); ZipEntry e = zip.AddFile(filename,""); e.Comment = "file " +filename+" added "+DateTime.Now; } Console.WriteLine("Done adding files to zip:" + zipName); zip.Comment = String.Format("This zip archive was created by '{0}' on '{1}'", System.Net.Dns.GetHostName(), DateTime.Now); zip.Save(zipName); Console.WriteLine("Zip made:" + zipName); } 
+4
source

All Articles