DERYANNEK
DERYANNEK

Reputation: 47

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

I want to decompress .7z-Files that I'm downloading from a Webserver. I already searched for a Solution but I only found somewhere I have to have 7zip installed.

Is there a simple way to decompress this files?

I already have a "foreach" that loops through items in a list containing the filenames.

Upvotes: 0

Views: 7659

Answers (3)

Sandip
Sandip

Reputation: 13

Decompress the .7z file with .Net core 2.2 Download SharpCompress(0.23.0) from nuget.

static void SharpCompressEx(string sevenZPath)
    {
        using (Stream stream = File.OpenRead(sevenZPath))
        {
            using (var archive = ArchiveFactory.Open(stream))
            {
                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                        entry.WriteToDirectory(@"C:\Temp", new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
                }
            }
        }
    }

Upvotes: 1

Aximili
Aximili

Reputation: 29444

I just tried the new SharpCompress and found it great if you don't care about the file type (eg. it can be 7z/zip/rar).

using (var archive = ArchiveFactory.Open(compressedFile))
{
    foreach (var entry in archive.Entries)
    {
        if (!entry.IsDirectory)
            entry.WriteToDirectory(@"C:\Temp", new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
    }
}

Upvotes: 6

Maximilian Gerhardt
Maximilian Gerhardt

Reputation: 5353

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.

Upvotes: 2

Related Questions