0

I have referred this Return StreamReader to Beginning, but couldn't figure out this problem.

This is code to read stream of a particular file in zip file. Here there are two stream of files inside two different zip files. Now I need to compare the streams.

I am unable to set the stream of BaseFileReader stream to beginning of stream.

using (FileStream BaseZipToOpen = new FileStream(BaseArchive,FileMode.Open) , CurrentZipToOpen = new FileStream(CurrentArchive,FileMode.Open))
{
    using (ZipArchive BaseZip = new ZipArchive(BaseZipToOpen, ZipArchiveMode.Read), CurrentZip = new ZipArchive(CurrentZipToOpen, ZipArchiveMode.Read))
    {
         ZipArchiveEntry BaseFile = BaseZip.GetEntry(requiredFile);
         ZipArchiveEntry CurrentFile = CurrentZip.GetEntry(requiredFile);

         using (StreamReader BaseFileReader = new StreamReader(BaseFile.Open()), CurrentFileReader = new StreamReader(CurrentFile.Open()))
         {
            string baseFileLine, currentFileLine;

            while (!CurrentFileReader.EndOfStream)
            {
                currentFileLine = CurrentFileReader.ReadLine();

                while (!BaseFileReader.EndOfStream)
                {
                    baseFileLine = BaseFileReader.ReadLine();

                    if (!currentFileLine.Equals(baseFileLine))
                    {
                        difference = true;
                    }
                    else 
                    {
                        difference = false;
                        break;
                    }
                }

                // how to reset BaseFileReader Stream to beginning?
                BaseZipToOpen.Seek(0, SeekOrigin.Begin); //This is not working
            }
        }
    }
}
Community
  • 1
  • 1
Tarun Kumar
  • 729
  • 1
  • 8
  • 16
  • What exactly are you trying to achieve? – JanR Mar 01 '16 at 05:24
  • How about close the current stream and create a new one? – Ian Mar 01 '16 at 05:24
  • I need to compare two text files which are in two different zip files and log the differences between them – Tarun Kumar Mar 01 '16 at 05:25
  • why not unzip the files, do the comparison and remove the unzipped files? – JanR Mar 01 '16 at 05:26
  • Yes that's another way of doing it bt for that I need to extract these to some temp location and then delete after comparing. And the size of these files can be large. – Tarun Kumar Mar 01 '16 at 05:29
  • I know my logic of comparing the streams is not perfect . Bt there must be a way to do like this also!! – Tarun Kumar Mar 01 '16 at 05:30
  • having all these nested streamreaders is a bad idea imo – JanR Mar 01 '16 at 05:47
  • This is the only way I got (from msdn) to get the stream of a file within archive. btw @Ian your idea worked . Before I was not able to create a new object of baseFileReader as it was in Scope of using() – Tarun Kumar Mar 01 '16 at 06:07
  • Thanks @Ian now the program is working bt still I am wondering how to reset the stream in this case – Tarun Kumar Mar 01 '16 at 06:14
  • @eTarun then probably you want to update your post with the new info. :) – Ian Mar 01 '16 at 06:16

1 Answers1

1

You can use

    FileStream stream = new FileStream();
    stream.Position = 0;