How to Create 7-Zip Archives with .Net

How do I create 7-Zip archives with .NET?

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.

How do I create 7-Zip archives containing multiple files with .NET?

At Last I decided to use SevenZipSharp. It has simple classes to Compress and Decompress Multiple Files, Single Files or bytes or Streams.

How to decompress a 7z archive with C# .NET 3.5?

I have come to the following conclusion:

Decompressing a .7z archive into a folder is not a feature provided by the C# LZMA SDK.

I'm afraid I have not found any hard evidence, like a blog post directly stating it.

However, I have found a description of the .7z file format. This description did not provide enough technical detail to craft a working solution, but it contained certain facts about the format:

In this section I will explain how the previous example is stored in the data Structured[...]

First up is the ​Header ​this contains just two variables: ​FileInfo​ and ​StreamsInfo.

FileInfo​ is probably the easiest structure to understand. It contains the name of each file in the 7z and a couple of other flags defining if the file is actually a directory or if the file is an empty file.

This structure is not reflected in the code of the C# SDK as far as I can see. That, combined with the information that the Decoder.Code() methods do not work with a directory path or more than one output Stream is a strong indicator.

So it seems that the C# part of the LZMA SDK only handles en- and decryption of a single file, or "Compressed Stream" as it is called in the linked document. The .7z archive format then counts as a separate format, which is ultimately a structure to contain an arbitrary number of compressed streams.

These streams can actually be combined in, again, arbitrary ways; e.g. an archive can contain files A and B compressed with LZMA, then add file C to the mix and compress it again with another algorithm. That makes decompressing a .7z archive more complex, which makes it even more of a pity that this functionality is not provided in the SDK.

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

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

Zip multiple files using 7zip.exe in C#

The command line that you want to create looks like

Sample Image

plus the required switches (arguments quoted and space delimited).

String.Join or StringBuilder are some coding things that may be helpful

I want to decompress .7z-Files without having 7zip installed

I personally had a good experience with the SevenZipSharp library.
https://sevenzipsharp.codeplex.com/

Code example for extracting / decompressing:

using (var tmp = new SevenZipExtractor(@"d:\Temp\7z465_extra.7z"))
{
for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
{
tmp.ExtractFiles(@"d:\Temp\Result\", tmp.ArchiveFileData[i].Index);
}
}

Just place the needed dll's from 7zip and this library in your programm folder and you're good to go. Therefore, you don't explicitly need 7zip to be installed, you just need the libs.



Related Topics



Leave a reply



Submit