detect string encoding when previewing .zip files, fix #24.

This commit is contained in:
Paddy Xu
2017-07-23 20:16:57 +03:00
parent 44f52db289
commit 7b6fa41baf
4 changed files with 59 additions and 9 deletions

View File

@@ -101,7 +101,7 @@ namespace QuickLook.Plugin.ArchiveViewer
if (useReader.Any(i => path.EndsWith(i))) if (useReader.Any(i => path.EndsWith(i)))
{ {
var reader = ReaderFactory.Open(stream); var reader = ReaderFactory.Open(stream, new ChardetReaderOptions());
_type = reader.ArchiveType.ToString(); _type = reader.ArchiveType.ToString();
@@ -113,7 +113,7 @@ namespace QuickLook.Plugin.ArchiveViewer
} }
else else
{ {
var archive = ArchiveFactory.Open(stream); var archive = ArchiveFactory.Open(stream, new ChardetReaderOptions());
_type = archive.Type.ToString(); _type = archive.Type.ToString();
@@ -138,8 +138,7 @@ namespace QuickLook.Plugin.ArchiveViewer
if (_fileEntries.ContainsKey(f)) if (_fileEntries.ContainsKey(f))
return; return;
ArchiveFileEntry parent; _fileEntries.TryGetValue(GetDirectoryName(f), out ArchiveFileEntry parent);
_fileEntries.TryGetValue(GetDirectoryName(f), out parent);
var afe = new ArchiveFileEntry(Path.GetFileName(f), true, parent); var afe = new ArchiveFileEntry(Path.GetFileName(f), true, parent);
@@ -151,8 +150,7 @@ namespace QuickLook.Plugin.ArchiveViewer
{ {
var file = pf.Last(); var file = pf.Last();
ArchiveFileEntry parent; _fileEntries.TryGetValue(GetDirectoryName(file), out ArchiveFileEntry parent);
_fileEntries.TryGetValue(GetDirectoryName(file), out parent);
_fileEntries.Add(file, new ArchiveFileEntry(Path.GetFileName(entry.Key), false, parent) _fileEntries.Add(file, new ArchiveFileEntry(Path.GetFileName(entry.Key), false, parent)
{ {

View File

@@ -0,0 +1,47 @@
// Copyright © 2017 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Text;
using SharpCompress.Common;
using SharpCompress.Readers;
using UtfUnknown;
namespace QuickLook.Plugin.ArchiveViewer
{
internal class ChardetReaderOptions : ReaderOptions
{
public ChardetReaderOptions()
{
ArchiveEncoding = new ArchiveEncoding
{
CustomDecoder = Chardet
};
}
public string Chardet(byte[] bytes, int index, int count)
{
var buffer = new byte[count];
Array.Copy(bytes, index, buffer, 0, count);
var encoding = CharsetDetector.DetectFromBytes(buffer).Detected?.Encoding ?? Encoding.Default;
return encoding.GetString(buffer);
}
}
}

View File

@@ -52,13 +52,16 @@
<ItemGroup> <ItemGroup>
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
<Reference Include="SharpCompress, Version=0.17.1.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL"> <Reference Include="SharpCompress, Version=0.18.0.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
<HintPath>..\..\packages\SharpCompress.0.17.1\lib\net45\SharpCompress.dll</HintPath> <HintPath>..\..\packages\SharpCompress.0.18.0\lib\net45\SharpCompress.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Xaml" /> <Reference Include="System.Xaml" />
<Reference Include="UtfUnknown, Version=1.0.0.0, Culture=neutral, PublicKeyToken=90217ce7a23260d4, processorArchitecture=MSIL">
<HintPath>..\..\packages\UTF.Unknown.1.0.0-beta1\lib\net40\UtfUnknown.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -69,6 +72,7 @@
<DependentUpon>ArchiveInfoPanel.xaml</DependentUpon> <DependentUpon>ArchiveInfoPanel.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="ArchiveFileEntry.cs" /> <Compile Include="ArchiveFileEntry.cs" />
<Compile Include="ChardetReaderOptions.cs" />
<Compile Include="IconManager.cs" /> <Compile Include="IconManager.cs" />
<Compile Include="Extensions.cs" /> <Compile Include="Extensions.cs" />
<Compile Include="Converters.cs" /> <Compile Include="Converters.cs" />

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="SharpCompress" version="0.17.1" targetFramework="net462" /> <package id="SharpCompress" version="0.18.0" targetFramework="net462" />
<package id="UTF.Unknown" version="1.0.0-beta1" targetFramework="net462" />
</packages> </packages>