Repository navigation
Closed
Description
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
KalleOlaviNiemitalo commentedon Sep 17, 2021
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.ashcor commentedon Sep 20, 2021
@KalleOlaviNiemitalo yes, this could likely be related. When changing the code to read full lines
the last line of the file is only partially read:
With a different parser, eg CsvHelper
the last two lines are read as expected:
KalleOlaviNiemitalo commentedon Sep 20, 2021
Assuming the bug is indeed in Microsoft.VisualBasic.FileIO.TextFieldParser, I think this issue should be in dotnet/runtime with area-Microsoft.VisualBasic.
cston commentedon Sep 22, 2021
See also #58857.
cston commentedon Sep 24, 2021
@ashcor, thanks for logging this issue!