Fix CSV viewer not support formats other than UTF8

This may have the disadvantage of slowing down the startup speed of large CSV files.
This commit is contained in:
ema
2024-12-13 22:12:14 +08:00
parent 302aad41ea
commit 4ab015eea8
2 changed files with 10 additions and 4 deletions

View File

@@ -22,10 +22,12 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Media; using System.Windows.Media;
using UtfUnknown;
namespace QuickLook.Plugin.CsvViewer; namespace QuickLook.Plugin.CsvViewer;
@@ -39,14 +41,17 @@ public partial class CsvViewerPanel : UserControl
InitializeComponent(); InitializeComponent();
} }
public List<string[]> Rows { get; private set; } = new List<string[]>(); public List<string[]> Rows { get; private set; } = [];
public void LoadFile(string path) public void LoadFile(string path)
{ {
const int limit = 10000; const int limit = 10000;
var binded = false; var binded = false;
using (var sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) var encoding = CharsetDetector.DetectFromFile(path).Detected?.Encoding ??
Encoding.Default;
using (var sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), encoding))
{ {
var conf = new CsvConfiguration(CultureInfo.CurrentUICulture) { MissingFieldFound = null, BadDataFound = null, DetectDelimiter = true }; var conf = new CsvConfiguration(CultureInfo.CurrentUICulture) { MissingFieldFound = null, BadDataFound = null, DetectDelimiter = true };
@@ -58,7 +63,7 @@ public partial class CsvViewerPanel : UserControl
var row = parser.Record; var row = parser.Record;
if (row == null) if (row == null)
break; break;
row = Concat(new[] { $"{i++ + 1}".PadLeft(6) }, row); row = Concat([$"{i++ + 1}".PadLeft(6)], row);
if (!binded) if (!binded)
{ {

View File

@@ -52,7 +52,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CsvHelper" Version="27.1.1" /> <PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="UTF.Unknown" Version="2.5.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>