Skip to content

Parsing CSV (TextFieldParser) from ZipArchive fails with .net 6 RC1.  #59371

Closed

Description

@ashcor

Describe the bug

Parsing a CSV file extracted from a ZipArchive fails with a MalformedLineExceptionin .net 6 RC 1.

         class Program
       {
        static void Main(string[] args)
        {
            Console.WriteLine($"Lines: {ReadCsv().Count()}");
        }

        public static IEnumerable<string[]> ReadCsv()
        {
            using (var zipStream = File.OpenRead("stops.zip"))
            using (var archive = new ZipArchive(zipStream))
            using (var stream = archive.GetEntry("stops.txt").Open())
            using (var reader = new StreamReader(stream))
            using (var parser = new TextFieldParser(reader)
            {
                TextFieldType = FieldType.Delimited,
                Delimiters = new string[] { "," },
                HasFieldsEnclosedInQuotes = true,
            })
            {
                while (!parser.EndOfData)
                    yield return parser.ReadFields();
            }
        }
    }

To Reproduce

clone https://github.com/ashcor/net6parse/tree/main/ConsoleApp1 and do dotnet run. The code fails with .net 6 RC1 but works fine when changing to <TargetFramework>net5.0</TargetFramework>. The zip file in question was produced with 7zip with the default settings. The encoding of the zipped file is UTF-8 BOM.

Exceptions (if any)

PS C:\Users\ashco\Documents\Github\net6parse\ConsoleApp1> dotnet run
Unhandled exception. Microsoft.VisualBasic.FileIO.MalformedLineException: Line 39136 cannot be parsed using the current Delimiters.
   at Microsoft.VisualBasic.FileIO.TextFieldParser.ParseDelimitedLine()
   at Microsoft.VisualBasic.FileIO.TextFieldParser.ReadFields()
   at ConsoleApp1.Program.ReadCsv()+MoveNext() in C:\Users\ashco\Documents\Github\net6parse\ConsoleApp1\Program.cs:line 31
   at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\ashco\Documents\Github\net6parse\ConsoleApp1\Program.cs:line 14 Line Number:39136

Further technical details

dotnet --info

PS C:\Users\ashco\Documents\Github\net6parse\ConsoleApp1> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.1.21458.32
 Commit:    d7c22323c4

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-rc.1.21458.32\

Host (useful for support):
  Version: 6.0.0-rc.1.21451.13
  Commit:  d7619cd4b1

Activity

  1. KalleOlaviNiemitalo commented on Sep 17, 2021

    @KalleOlaviNiemitalo

    Likely related to Breaking change: Partial and zero-byte reads in DeflateStream, GZipStream, and CryptoStream - .NET and #53644. The TextFieldParser.ReadNextLine method appears to correctly loop calling ByVal ChangeBuffer As ChangeBufferFunction, i.e. either IncreaseBufferSize or ReadToBuffer, until it gets vbCr, vbLf, or a zero-length read; but perhaps there is some problem that I'm not noticing.

  2. ashcor commented on Sep 20, 2021

    @ashcor
    Author

    @KalleOlaviNiemitalo yes, this could likely be related. When changing the code to read full lines

    while (!parser.EndOfData)
            yield return parser.ReadLine();
    

    the last line of the file is only partially read:

    "8793698","Buswiller-Eglise","48.8194726014747","7.55968244303174","",""
    "8799
    

    With a different parser, eg CsvHelper

     using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
      {
          while (csv.Read())
              yield return csv.Parser.RawRecord;
      }
    

    the last two lines are read as expected:

    "8793698","Buswiller-Eglise","48.8194726014747","7.55968244303174","",""
    "8799803","Annecy, gare routière","45.9020779242132","6.11820082216869","",""
    
  3. removed their assignment
    on Sep 20, 2021
  4. transferred this issue fromdotnet/sdkon Sep 20, 2021
  5. KalleOlaviNiemitalo commented on Sep 20, 2021

    @KalleOlaviNiemitalo

    Assuming the bug is indeed in Microsoft.VisualBasic.FileIO.TextFieldParser, I think this issue should be in dotnet/runtime with area-Microsoft.VisualBasic.

  6. transferred this issue fromdotnet/roslynon Sep 20, 2021
  7. ghost added
    untriaged
    on Sep 20, 2021
  8. added this to the 6.0.0 milestone on Sep 21, 2021
  9. removed
    untriaged
    on Sep 21, 2021
  10. cston commented on Sep 22, 2021

    @cston
    Contributor

    See also #58857.

  11. self-assigned this
    on Sep 22, 2021
  12. self-assigned this
    on Sep 22, 2021
  13. ghost added
    in-pr
    on Sep 22, 2021
  14. removed their assignment
    on Sep 22, 2021
  15. ghost removed
    in-pr
    on Sep 22, 2021
  16. cston commented on Sep 24, 2021

    @cston
    Contributor

    @ashcor, thanks for logging this issue!

  17. ghost locked as resolved and limited conversation to collaborators on Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions