简体   繁体   English

将DotNetZip内存流转换为字符串

[英]Converting DotNetZip memory stream to string

I am trying to read a file within a zip to check if that file has a certain string in it. 我正在尝试读取zip文件,以检查该文件中是否包含某个字符串。 But I can seem to get the "file" (memory stream) into a string in order to search it. 但是我似乎可以将“文件”(内存流)放入字符串中以进行搜索。

When I use the following code "stringOfStream" is always blank, what am I doing wrong? 当我使用以下代码“ stringOfStream”始终为空时,我在做什么错? The reader always has a length and read byte returns different numbers. 读取器始终具有长度,读取的字节返回不同的数字。

        using (ZipFile zip = ZipFile.Read(currentFile.FullName))
        {
            ZipEntry e = zip[this.searchFile.Text];

            using (MemoryStream reader = new MemoryStream())
            {
                e.Extract(reader);
                var stringReader = new StreamReader(reader);
                var stringOfStream = stringReader.ReadToEnd();
            }

        }

Thanks 谢谢

I think when you call Extract the position of the stream goes to the end of the file, so you need to reposition again to get the data. 我认为,当您调用Extract时,流的位置将到达文件的末尾,因此您需要再次重新定位以获取数据。

Can you try this please : 你可以试试这个吗:

 using (ZipFile zip = ZipFile.Read(currentFile.FullName))
 {
            ZipEntry e = zip[this.searchFile.Text];

            using (MemoryStream reader = new MemoryStream())
            {
                e.Extract(reader);
                reader.Position = 0;
                var stringReader = new StreamReader(reader);
                var stringOfStream = stringReader.ReadToEnd();
            }

  }

Check if it works or not. 检查它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM