Usage of Seven Zip in C#

Compresss by Squid-Box.SevenZipSharp.Lite

choose x86/x64 7z dll


if (IntPtr.Size == 4)
{
    SevenZipExtractor.SetLibraryPath(@"x86\7z.dll");
}
else
{
    SevenZipExtractor.SetLibraryPath(@"x64\7z.dll");
}
if (IntPtr.Size == 4)
{
    SevenZipCompressor.SetLibraryPath(@"x86\7z.dll");
}
else
{
    SevenZipCompressor.SetLibraryPath(@"x64\7z.dll");
}

Decompression As File

using (SevenZipExtractor tmp = new SevenZipExtractor("Name.7z")))
{
    for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
    {
        tmp.ExtractFiles("Path to sotre decompression file", tmp.ArchiveFileData[i].Index);
    }
}

Compress to memoryStream

SevenZipCompressor ziper = new SevenZipCompressor()
{
    ArchiveFormat = OutArchiveFormat.SevenZip,
    DirectoryStructure = false
};
using (MemoryStream zippedStream = new MemoryStream())
{
    ziper.DefaultItemName = "DefaultItemName ";
    ziper.CompressStream(InputOrginalStream, ZipedOutStream);
    using (FileStream fs = new FileStream("Path to store", FileMode.Create))
    {
        using (BinaryWriter bw = new BinaryWriter(fs))
        {
            bw.Write(zippedStream.ToArray());
        }
    }
}

Extract file to memeory stream (nuget SevenZipExtractor)

using SevenZipExtractor;
MemoryStream outStream = new MemoryStream();
using (ArchiveFile archiveFile = new ArchiveFile("path of zipped file"))
{
    foreach (Entry entry in archiveFile.Entries)
    {
        //Console.WriteLine(entry.FileName);
        //Console.WriteLine("Extract" + outStream.Length);
        entry.Extract(outStream);
    }
}
posted @ 2022-08-10 15:14  supersdar  阅读(101)  评论(0编辑  收藏  举报