0

I have a zip file with a csv file inside it. I am using following code to read the file:

    using (ZipArchive zipArchive = ZipFile.OpenRead(filePath))
    {
    var zipArchiveEntry = zipArchive.GetEntry("File.csv");
    var zipEntry = zipArchiveEntry.Open();
...
    }

The zipEntry is of type System.IO.Compreesion.Deflatestream.

I tried using StreamReader.CurrentEncoding, but its giving wrong encoding value.

I am using this solution now,

this.GetFileEncoding(zipEntry)

but getting NotSupportedException at fileStrem.Length.

How do i find the right Encoding of the zipEntry (File.csv)?

lerner1225
  • 862
  • 7
  • 25
  • 1
    You have to extract (unzip, see [msdn](https://msdn.microsoft.com/en-us/library/hh485716(v=vs.110).aspx)) file, not decode stream. – Sinatr Sep 26 '17 at 11:48
  • Not allowed to extract and save the file to disk :( – lerner1225 Sep 26 '17 at 11:50
  • It should be possible to extract into e.g. memory stream, see [this](https://stackoverflow.com/a/10599797/1997232) (it's compress method, shouldn't be hard to make opposite one). – Sinatr Sep 26 '17 at 11:51
  • Already tried memorystream. In later steps i am using TextFieldParser to read the csv file memorystream. Following exception occurrs: The stream passed to TextFieldParser cannot be read. (This may be a result of attempting to read a file that is not a text file.) – lerner1225 Sep 26 '17 at 12:08

1 Answers1

0

Provided you can assure that the file is in fact a cvs file then just use a stream reader to get it contents from the entry's stream when opened

using (ZipArchive archive = ZipFile.OpenRead(filePath)) {
    var entry = archive.GetEntry("File.csv");
    if (entry != null) {
        using (var reader = new StreamReader(entry.Open())) {
            var csv = reader.ReadToEnd();
        }
    }    
} 
Nkosi
  • 235,767
  • 35
  • 427
  • 472