英文:
C# How to read lines from a Stream object
问题
我正在尝试从一个Zip文件中读取前两行.txt文件的内容。我不需要保存整个文件到磁盘。我可以轻松访问zip存档实体:
ZipArchive archive = ZipFile.OpenRead(fileName);
ZipArchiveEntry en = archive.Entries.FirstOrDefault(e =>
Path.GetExtension(e.Name) == ".txt");
使用
en.Open()
只返回一个流(Stream),而不是StreamReader,因此我无法使用
en.ReadLine()
那么我该如何只读取前两行呢?
谢谢。
英文:
I am trying to read the first two lines from a .txt file inside a Zip file. I don't want or need to save the whole file to disk. I can get to the zip archive entity easily enough:
ZipArchive archive = ZipFile.OpenRead(fileName);
ZipArchiveEntry en = archive.Entries.FirstOrDefault(e =>
Path.GetExtension(e.Name) == ".txt");
Using
en.Open()
only returns a Stream, not StreamReader so I can't use
en.ReadLine()
So how can I read just read the first two lines?
Thank you.
答案1
得分: 1
你可以参考这里获取一些知识。
但简而言之,只需创建一个StreamReader实例,参数是从"en.Open()"中获取的流。
using (var reader = new StreamReader(en.Open()))
英文:
You can refer some knowledge from here.
But in short, just create instance of StreamReader with param is Stream you got from "en.Open()"
using (var reader = new StreamReader(en.Open()))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论