C# - How do I create a regular ZIP archive using the 7-zip library(i.e. not a .7z, but .zip)?

StackOverflow https://stackoverflow.com/questions/5895937

  •  29-10-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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):

 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);
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top