COM is really a nightmare... Use managed solution (SharpCompress) instead

This commit is contained in:
Paddy Xu
2017-05-01 12:09:16 +03:00
parent e6b7e14bba
commit dbafec9cb8
20 changed files with 82 additions and 4390 deletions

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Controls;
using SevenZip;
using SharpCompress.Archives;
namespace QuickLook.Plugin.ArchiveViewer
{
@@ -65,23 +65,25 @@ namespace QuickLook.Plugin.ArchiveViewer
private void LoadItemsFromArchive(string path)
{
using (var reader = new SevenZipExtractor(path))
using (var stream = File.OpenRead(path))
{
_totalZippedSize = (ulong) reader.PackedSize;
_solid = reader.IsSolid;
_type = reader.Format.ToString();
var archive = ArchiveFactory.Open(stream);
_totalZippedSize = (ulong) archive.TotalSize;
_solid = archive.IsSolid;
_type = archive.Type.ToString();
var root = new ArchiveFileEntry(Path.GetFileName(path), true);
_fileEntries.Add("", root);
foreach (var entry in reader.ArchiveFileData)
foreach (var entry in archive.Entries)
ProcessByLevel(entry);
}
}
private void ProcessByLevel(ArchiveFileInfo entry)
private void ProcessByLevel(IArchiveEntry entry)
{
var pf = GetPathFragments(entry.FileName);
var pf = GetPathFragments(entry.Key);
// process folders. When entry is a directory, all fragments are folders.
pf.Take(entry.IsDirectory ? pf.Length : pf.Length - 1)
@@ -107,11 +109,11 @@ namespace QuickLook.Plugin.ArchiveViewer
ArchiveFileEntry parent;
_fileEntries.TryGetValue(GetDirectoryName(file), out parent);
_fileEntries.Add(file, new ArchiveFileEntry(Path.GetFileName(entry.FileName), false, parent)
_fileEntries.Add(file, new ArchiveFileEntry(Path.GetFileName(entry.Key), false, parent)
{
Encrypted = entry.Encrypted,
Size = entry.Size,
ModifiedDate = entry.LastWriteTime
Encrypted = entry.IsEncrypted,
Size = (ulong) entry.Size,
ModifiedDate = entry.LastModifiedTime ?? new DateTime()
});
}
}
@@ -125,7 +127,7 @@ namespace QuickLook.Plugin.ArchiveViewer
private string[] GetPathFragments(string path)
{
var frags = path.Split('\\');
var frags = path.Split('\\', '/').Where(f => !string.IsNullOrEmpty(f)).ToArray();
return frags.Select((s, i) => frags.Take(i + 1).Aggregate((a, b) => a + "\\" + b)).ToArray();
}

View File

@@ -9,6 +9,9 @@ namespace QuickLook.Plugin.ArchiveViewer
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] == DependencyProperty.UnsetValue)
values[0] = 1;
var level = (int) values[0];
var indent = (double) values[1];
return indent * level;

View File

@@ -1,7 +1,7 @@
using System;
using System.IO;
using System.Windows;
using SevenZip;
using SharpCompress.Archives;
namespace QuickLook.Plugin.ArchiveViewer
{
@@ -16,41 +16,26 @@ namespace QuickLook.Plugin.ArchiveViewer
if (Directory.Exists(path))
return false;
try
using (var stream = File.OpenRead(path))
{
using (var archive = new SevenZipExtractor(path))
try
{
// dummy access to the data. If it throws exception, return false
if (archive.ArchiveFileData == null)
return false;
// ignore some formats
switch (archive.Format)
{
case InArchiveFormat.Chm:
case InArchiveFormat.Flv:
case InArchiveFormat.Elf:
case InArchiveFormat.Msi:
case InArchiveFormat.PE:
case InArchiveFormat.Swf:
return false;
}
ArchiveFactory.Open(stream);
}
catch (Exception)
{
return false;
}
}
catch (Exception)
{
return false;
}
return true;
}
public void BoundViewSize(string path, ViewerObject context)
public void BoundViewSize(string path, ContextObject context)
{
context.PreferredSize = new Size {Width = 800, Height = 600};
}
public void View(string path, ViewerObject context)
public void View(string path, ContextObject context)
{
_panel = new ArchiveInfoPanel(path);

View File

@@ -34,9 +34,8 @@
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="SevenZipSharp">
<HintPath>References\SevenZipSharp.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
<Reference Include="SharpCompress, Version=0.15.2.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
<HintPath>..\..\packages\SharpCompress.0.15.2\lib\net45\SharpCompress.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
@@ -81,5 +80,8 @@
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SharpCompress" version="0.15.2" targetFramework="net452" />
</packages>