如何基本上用sevenzipsharp提取文件

时间:2015-01-31 20:50:33

标签: c# extract sevenzipsharp

我将使用sevenzipsharp从iso文件中将文件提取到usb。为此,我从vs nuget包管理器下载sevenzipsharp,我编码(实际上我不能:)这段代码。我不接受任何错误,但它不工作。我在哪里犯错误?请写详细信息。

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

提前谢谢

2 个答案:

答案 0 :(得分:3)

对于x86,您正在SevenZip.SevenZipCompressor.SetLibraryPath执行SevenZip.SevenZipExtractor.SetLibraryPath

答案 1 :(得分:0)

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();
                        //...
                    }
                }
            }
        }
    }
}