0

I am trying to read a file within a zip to check if that file has a certain string in it. 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? 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

chrispepper1989
  • 2,100
  • 2
  • 23
  • 48
  • 2
    I would try to set the position of the stream to 0 before calling `ReadToEnd`. – Florent B. Oct 03 '17 at 18:32
  • I believe Florent B. is correct, based on the answer here: https://stackoverflow.com/questions/13887538/how-to-use-dotnetzip-to-extract-xml-file-from-zip – Gabriel Luci Oct 03 '17 at 18:34

1 Answers1

2

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.

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.

abhishek
  • 2,975
  • 23
  • 38