SevenZipSharp,如何读取txt文件?

时间:2016-10-18 14:06:53

标签: c# 7zip sevenzipsharp

我正在尝试从.7z存档中读取txt文件

using (StreamReader reader = new StreamReader(f + "//" + file.FileName))

但是我收到了这个错误:

  

未处理的类型' System.IO.DirectoryNotFoundException'发生在mscorlib.dll中   附加信息:找不到路径的一部分' E:\ 1.7z \ 1 \ 2 \ 3 \ New Text Document.txt'。

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

            string f = "E://1.7z";
            SevenZipExtractor extractor = new SevenZipExtractor(f);

            foreach (ArchiveFileInfo file in extractor.ArchiveFileData)
            {
                // Console.WriteLine("{0} : {1} Bytes", file.FileName, file.Size);

                if (file.FileName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    using (StreamReader reader = new StreamReader(f + "//" + file.FileName))
                    {
                        while (reader.Peek() >= 0)
                        {
                            Console.WriteLine("{0} ", reader.ReadLine());
                        }
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

要使用SevenZipExtractor提取文件,请使用以下命令:

String file = @"\\yourdirectory\\yourzipfile.zip";  
String directoryToExtract = @"\\yourdirectorytoextract"
using (SevenZip.SevenZipExtractor extr = new SevenZip.SevenZipExtractor(file))
{
             Console.WriteLine("Extracting File...");
             extr.ExtractArchive(directoryToExtract);
             System.IO.File.Delete(file);   
}