4

I will extract files to usb from iso file with sevenzipsharp. For this, I download sevenzipsharp from vs nuget package manager and I coded (actually I couldn't :) ) this code . I dont take any error but It isnt working. Where do I make mistakes? Please write details.

if (IntPtr.Size == 8) //x64
{
    SevenZip.SevenZipExtractor.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll");
}
else //x86
{
    SevenZip.SevenZipCompressor.SetLibraryPath(@"C:\Program Files (x86)\7-Zip\7z.dll");
}
using (var file = new SevenZipExtractor(sourcePath))
{
    file.ExtractArchive(outputPath);  
}

Thank you in advance

Haydar ŞAHİN
  • 614
  • 1
  • 7
  • 12
  • 2
    Could you edit your answer and be more specific? What error messages do you get? – Dave Jan 31 '15 at 20:54
  • Even after that edit, it's unclear what your question is. `"I dont take any error. Where do I make mistakes? Please write details."` makes zero sense from any angle. – B.K. Jan 31 '15 at 21:21
  • hi again Dear B.K I'm uncomfortable with this too but i have no idea why it isnt working and again i have no idea what i can share with you because its not returning any result, error or something. I just want to extract an iso file to directory with c#, can you guys help me about that? – Haydar ŞAHİN Jan 31 '15 at 22:02
  • @HaydarŞAHİN I would step through the code and make sure that `sourcePath` and `outputPath` are not null. I would also make sure that the path you're setting your SevenZip library path to the one that actually exists. If you're running on x64 and expect it to have int pointer size of 8, make sure that you are building for x64 or any CPU. – B.K. Feb 01 '15 at 18:02

2 Answers2

3

For x86 you are doing SevenZip.SevenZipCompressor.SetLibraryPath where you probably meant to do SevenZip.SevenZipExtractor.SetLibraryPath.

tomsv
  • 7,207
  • 6
  • 55
  • 88
3
class Program
{
    const string zipFile = @"D:\downloads\price.zip";

    static void Main(string[] args)
    {
        using (Stream stream = File.OpenRead(zipFile))
        {
            string dllPath = Environment.Is64BitProcess ?
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z64.dll")
                    : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z.dll");

            SevenZipBase.SetLibraryPath(dllPath);

            Extract(stream);
        }
    }

    static void Extract(Stream archiveStream)
    {
        using (SevenZipExtractor extr = new SevenZipExtractor(archiveStream))
        {
            foreach (ArchiveFileInfo archiveFileInfo in extr.ArchiveFileData)
            {
                if (!archiveFileInfo.IsDirectory)
                {
                    using (var mem = new MemoryStream())
                    {
                        extr.ExtractFile(archiveFileInfo.Index, mem);

                        string shortFileName = Path.GetFileName(archiveFileInfo.FileName);
                        byte[] content = mem.ToArray();
                        //...
                    }
                }
            }
        }
    }
}
Miles
  • 488
  • 5
  • 19