mirror of
https://github.com/QL-Win/QuickLook.git
synced 2026-01-14 07:06:15 +08:00
Move archive viewer files to ArchiveFile namespace
Renamed and reorganized ArchiveViewer files into a new ArchiveFile subfolder and namespace for better code structure. Updated all relevant namespaces and references accordingly. Minor code cleanups were also applied, such as using collection initializers and default keyword.
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// 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.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer.ArchiveFile;
|
||||
|
||||
public class ArchiveFileEntry : IComparable<ArchiveFileEntry>
|
||||
{
|
||||
private readonly ArchiveFileEntry _parent;
|
||||
private int _cachedDepth = -1;
|
||||
private int _cachedLevel = -1;
|
||||
|
||||
public ArchiveFileEntry(string name, bool isFolder, ArchiveFileEntry parent = null)
|
||||
{
|
||||
Name = name;
|
||||
IsFolder = isFolder;
|
||||
|
||||
_parent = parent;
|
||||
_parent?.Children.Add(this, false);
|
||||
}
|
||||
|
||||
public SortedList<ArchiveFileEntry, bool> Children { get; set; } = [];
|
||||
|
||||
public string Name { get; set; }
|
||||
public bool Encrypted { get; set; }
|
||||
public bool IsFolder { get; set; }
|
||||
public ulong Size { get; set; }
|
||||
public DateTime ModifiedDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the maximum depth of all siblings
|
||||
/// </summary>
|
||||
public int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_cachedLevel != -1)
|
||||
return _cachedLevel;
|
||||
|
||||
if (_parent == null)
|
||||
_cachedLevel = GetDepth();
|
||||
else
|
||||
_cachedLevel = _parent.Level - 1;
|
||||
|
||||
return _cachedLevel;
|
||||
}
|
||||
}
|
||||
|
||||
public int CompareTo(ArchiveFileEntry other)
|
||||
{
|
||||
if (IsFolder == other.IsFolder)
|
||||
return string.Compare(Name, other.Name, StringComparison.CurrentCulture);
|
||||
|
||||
if (IsFolder)
|
||||
return -1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of nodes in the longest path to a leaf
|
||||
/// </summary>
|
||||
private int GetDepth()
|
||||
{
|
||||
if (_cachedDepth != -1)
|
||||
return _cachedDepth;
|
||||
|
||||
var max = Children.Keys.Count == 0 ? 0 : Children.Keys.Max(r => r.GetDepth());
|
||||
_cachedDepth = max + 1;
|
||||
return _cachedDepth;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (IsFolder)
|
||||
return $"{Name}";
|
||||
|
||||
var en = Encrypted ? "*" : string.Empty;
|
||||
return $"{Name}{en},{IsFolder},{Size},{ModifiedDate}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<UserControl x:Class="QuickLook.Plugin.ArchiveViewer.ArchiveFile.ArchiveFileListView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:QuickLook.Plugin.ArchiveViewer.ArchiveFile"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
d:DesignHeight="300"
|
||||
d:DesignWidth="300"
|
||||
mc:Ignorable="d">
|
||||
<Grid x:Name="treeGrid" Grid.IsSharedSizeScope="True">
|
||||
<Grid.RowDefinitions>
|
||||
<!-- Header row -->
|
||||
<RowDefinition Height="Auto" />
|
||||
<!-- Row for data -->
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid.Resources>
|
||||
|
||||
<!-- Converts the level in the tree to the width of the spacer column -->
|
||||
<local:LevelToIndentConverter x:Key="LevelToIndentConverter" />
|
||||
<local:BooleanToAsteriskConverter x:Key="BooleanToAsteriskConverter" />
|
||||
<local:SizePrettyPrintConverter x:Key="SizePrettyPrintConverter" />
|
||||
<local:DatePrintConverter x:Key="DatePrintConverter" />
|
||||
<local:FileExtToIconConverter x:Key="FileExtToIconConverter" />
|
||||
<local:LevelToBooleanConverter x:Key="LevelToBooleanConverter" />
|
||||
|
||||
<!-- Template for directory information at all levels -->
|
||||
<HierarchicalDataTemplate DataType="{x:Type local:ArchiveFileEntry}" ItemsSource="{Binding Children.Keys}">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="2" />
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="2" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid Grid.Row="1" ShowGridLines="False">
|
||||
<!--
|
||||
All column widths are shared except for column 1 which is sized
|
||||
to compensate for different indentation at each level
|
||||
-->
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition MinWidth="300" SharedSizeGroup="rowHeaderColumn" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition MinWidth="100" SharedSizeGroup="column1" />
|
||||
<ColumnDefinition MinWidth="100" SharedSizeGroup="column2" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0" Orientation="Horizontal">
|
||||
<Image Width="16" Height="16">
|
||||
<Image.Source>
|
||||
<MultiBinding Converter="{StaticResource FileExtToIconConverter}">
|
||||
<Binding Path="Name" />
|
||||
<Binding Path="IsFolder" />
|
||||
</MultiBinding>
|
||||
</Image.Source>
|
||||
</Image>
|
||||
<TextBlock Text="{Binding Name}" />
|
||||
<TextBlock Text="{Binding Encrypted, Converter={StaticResource BooleanToAsteriskConverter}}" />
|
||||
</StackPanel>
|
||||
<Rectangle Grid.Column="1">
|
||||
<Rectangle.Width>
|
||||
<MultiBinding Converter="{StaticResource LevelToIndentConverter}">
|
||||
<Binding Path="Level" />
|
||||
<Binding ElementName="treeViewItemToMeasure" Path="ActualWidth" />
|
||||
</MultiBinding>
|
||||
</Rectangle.Width>
|
||||
</Rectangle>
|
||||
<TextBlock Grid.Column="2">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding Converter="{StaticResource SizePrettyPrintConverter}">
|
||||
<Binding Path="Size" />
|
||||
<Binding Path="IsFolder" />
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock Grid.Column="3">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding Converter="{StaticResource DatePrintConverter}">
|
||||
<Binding Path="ModifiedDate" />
|
||||
<Binding Path="IsFolder" />
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</HierarchicalDataTemplate>
|
||||
</Grid.Resources>
|
||||
|
||||
<!-- Tree view with one item for the header row -->
|
||||
|
||||
<TreeView Background="Transparent"
|
||||
BorderThickness="0"
|
||||
Focusable="False"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
|
||||
<TreeViewItem Focusable="False" Visibility="Collapsed">
|
||||
<TreeViewItem.Header>
|
||||
<Grid ShowGridLines="False">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition SharedSizeGroup="rowHeaderColumn" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition SharedSizeGroup="column1" />
|
||||
<ColumnDefinition SharedSizeGroup="column2" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text=" " />
|
||||
<TreeViewItem Grid.Column="1">
|
||||
<TreeViewItem.Header>
|
||||
<TreeViewItem x:Name="treeViewItemToMeasure" Padding="0" />
|
||||
</TreeViewItem.Header>
|
||||
|
||||
<!--
|
||||
Set the width of Column 1 to the same width as the top level
|
||||
in the data
|
||||
-->
|
||||
<TreeViewItem.Width>
|
||||
<MultiBinding Converter="{StaticResource LevelToIndentConverter}">
|
||||
<Binding Path="Level" />
|
||||
<Binding ElementName="treeViewItemToMeasure" Path="ActualWidth" />
|
||||
</MultiBinding>
|
||||
</TreeViewItem.Width>
|
||||
</TreeViewItem>
|
||||
<TextBlock Grid.Column="2" Text="Original Size" />
|
||||
<TextBlock Grid.Column="3" Text="Modified Date" />
|
||||
</Grid>
|
||||
</TreeViewItem.Header>
|
||||
</TreeViewItem>
|
||||
</TreeView>
|
||||
|
||||
<!-- Tree view that will display hierarchical data rows -->
|
||||
|
||||
<TreeView x:Name="treeView"
|
||||
Grid.Row="1"
|
||||
ItemsSource="{Binding}" />
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// 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 QuickLook.Plugin.ArchiveViewer.ArchiveFile;
|
||||
using System;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer.ArchiveFile;
|
||||
|
||||
public partial class ArchiveFileListView : UserControl, IDisposable
|
||||
{
|
||||
public ArchiveFileListView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
IconManager.ClearCache();
|
||||
}
|
||||
|
||||
public void SetDataContext(object context)
|
||||
{
|
||||
treeGrid.DataContext = context;
|
||||
|
||||
treeView.LayoutUpdated += (sender, e) =>
|
||||
{
|
||||
// return when empty
|
||||
if (treeView.Items.Count == 0)
|
||||
return;
|
||||
|
||||
// return when there are more than one root nodes
|
||||
if (treeView.Items.Count > 1)
|
||||
return;
|
||||
|
||||
var root = (TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(treeView.Items[0]);
|
||||
if (root == null)
|
||||
return;
|
||||
|
||||
root.IsExpanded = true;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<UserControl x:Class="QuickLook.Plugin.ArchiveViewer.ArchiveFile.ArchiveInfoPanel"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:QuickLook.Plugin.ArchiveViewer.ArchiveFile"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
x:Name="infoPanel"
|
||||
d:DesignHeight="600"
|
||||
d:DesignWidth="800"
|
||||
mc:Ignorable="d">
|
||||
<UserControl.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<!-- only for design -->
|
||||
<ResourceDictionary Source="/QuickLook.Common;component/Styles/MainWindowStyles.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<local:Percent100ToVisibilityVisibleConverter x:Key="Percent100ToVisibilityVisibleConverter" />
|
||||
<local:Percent100ToVisibilityCollapsedConverter x:Key="Percent100ToVisibilityCollapsedConverter" />
|
||||
</ResourceDictionary>
|
||||
</UserControl.Resources>
|
||||
<Grid>
|
||||
<Grid Visibility="{Binding ElementName=infoPanel, Path=LoadPercent, Mode=OneWay, Converter={StaticResource Percent100ToVisibilityCollapsedConverter}}" ZIndex="9999">
|
||||
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
|
||||
<Label x:Name="lblLoading"
|
||||
HorizontalAlignment="Center"
|
||||
FontSize="14"
|
||||
Foreground="{DynamicResource WindowTextForeground}">
|
||||
Loading archive ...
|
||||
</Label>
|
||||
<ProgressBar Width="150"
|
||||
Height="13"
|
||||
Value="{Binding ElementName=infoPanel, Path=LoadPercent, Mode=OneWay}" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
<Grid Visibility="{Binding ElementName=infoPanel, Path=LoadPercent, Mode=OneWay, Converter={StaticResource Percent100ToVisibilityVisibleConverter}}">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="30" />
|
||||
</Grid.RowDefinitions>
|
||||
<local:ArchiveFileListView x:Name="fileListView"
|
||||
Grid.Row="0"
|
||||
Focusable="False"
|
||||
Foreground="{DynamicResource WindowTextForeground}" />
|
||||
<Grid Grid.Row="1">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="40*" />
|
||||
<ColumnDefinition Width="30*" />
|
||||
<ColumnDefinition Width="30*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Label x:Name="archiveCount"
|
||||
Grid.Column="0"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="{DynamicResource WindowTextForegroundAlternative}">
|
||||
0 folders and 0 files, solid, password-protected
|
||||
</Label>
|
||||
<Label x:Name="archiveSizeC"
|
||||
Grid.Column="1"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="{DynamicResource WindowTextForegroundAlternative}">
|
||||
Compressed size 0 bytes
|
||||
</Label>
|
||||
<Label x:Name="archiveSizeU"
|
||||
Grid.Column="2"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="{DynamicResource WindowTextForegroundAlternative}">
|
||||
Uncompressed size 0 bytes
|
||||
</Label>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,229 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// 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 PureSharpCompress.Archives;
|
||||
using PureSharpCompress.Common;
|
||||
using PureSharpCompress.Readers;
|
||||
using QuickLook.Common.Annotations;
|
||||
using QuickLook.Common.ExtensionMethods;
|
||||
using QuickLook.Common.Helpers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer.ArchiveFile;
|
||||
|
||||
public partial class ArchiveInfoPanel : UserControl, IDisposable, INotifyPropertyChanged
|
||||
{
|
||||
private readonly Dictionary<string, ArchiveFileEntry> _fileEntries = [];
|
||||
private bool _disposed;
|
||||
private double _loadPercent;
|
||||
private ulong _totalZippedSize;
|
||||
private string _type;
|
||||
|
||||
public ArchiveInfoPanel(string path)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
// design-time only
|
||||
Resources.MergedDictionaries.Clear();
|
||||
|
||||
BeginLoadArchive(path);
|
||||
}
|
||||
|
||||
public double LoadPercent
|
||||
{
|
||||
get => _loadPercent;
|
||||
private set
|
||||
{
|
||||
if (value == _loadPercent) return;
|
||||
_loadPercent = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
_disposed = true;
|
||||
|
||||
fileListView.Dispose();
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
private void BeginLoadArchive(string path)
|
||||
{
|
||||
new Task(() =>
|
||||
{
|
||||
_totalZippedSize = (ulong)new FileInfo(path).Length;
|
||||
|
||||
var root = new ArchiveFileEntry(Path.GetFileName(path), true);
|
||||
_fileEntries.Add(string.Empty, root);
|
||||
|
||||
try
|
||||
{
|
||||
LoadItemsFromArchive(path);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProcessHelper.WriteLog(e.ToString());
|
||||
Dispatcher.Invoke(() => { lblLoading.Content = "Preview failed. See log for more details."; });
|
||||
return;
|
||||
}
|
||||
|
||||
var folders = -1; // do not count root node
|
||||
var files = 0;
|
||||
ulong sizeU = 0L;
|
||||
_fileEntries.ForEach(e =>
|
||||
{
|
||||
if (e.Value.IsFolder)
|
||||
folders++;
|
||||
else
|
||||
files++;
|
||||
|
||||
sizeU += e.Value.Size;
|
||||
});
|
||||
|
||||
string t;
|
||||
var d = folders != 0 ? $"{folders} folders" : string.Empty;
|
||||
var f = files != 0 ? $"{files} files" : string.Empty;
|
||||
if (!string.IsNullOrEmpty(d) && !string.IsNullOrEmpty(f))
|
||||
t = $", {d} and {f}";
|
||||
else if (string.IsNullOrEmpty(d) && string.IsNullOrEmpty(f))
|
||||
t = string.Empty;
|
||||
else
|
||||
t = $", {d}{f}";
|
||||
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
|
||||
fileListView.SetDataContext(_fileEntries[string.Empty].Children.Keys);
|
||||
archiveCount.Content =
|
||||
$"{_type} archive{t}";
|
||||
archiveSizeC.Content =
|
||||
$"Compressed size {((long)_totalZippedSize).ToPrettySize(2)}";
|
||||
archiveSizeU.Content = $"Uncompressed size {((long)sizeU).ToPrettySize(2)}";
|
||||
});
|
||||
|
||||
LoadPercent = 100d;
|
||||
}).Start();
|
||||
}
|
||||
|
||||
private void LoadItemsFromArchive(string path)
|
||||
{
|
||||
using var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
|
||||
// ReaderFactory is slow... so limit its usage
|
||||
string[] useReader = [".tar.gz", ".tgz", ".tar.bz2", ".tar.lz", ".tar.xz"];
|
||||
|
||||
if (useReader.Any(path.ToLower().EndsWith))
|
||||
{
|
||||
var reader = ReaderFactory.Open(fileStream, new ChardetReaderOptions());
|
||||
|
||||
_type = reader.ArchiveType.ToString();
|
||||
|
||||
while (reader.MoveToNextEntry())
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
LoadPercent = 100d * fileStream.Position / fileStream.Length;
|
||||
ProcessByLevel(reader.Entry);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var archive = ArchiveFactory.Open(fileStream, new ChardetReaderOptions());
|
||||
|
||||
_type = archive.Type.ToString();
|
||||
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
LoadPercent = 100d * fileStream.Position / fileStream.Length;
|
||||
ProcessByLevel(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessByLevel(IEntry entry)
|
||||
{
|
||||
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)
|
||||
.ForEach(f =>
|
||||
{
|
||||
// skip if current dir is already added
|
||||
if (_fileEntries.ContainsKey(f))
|
||||
return;
|
||||
|
||||
_fileEntries.TryGetValue(GetDirectoryName(f), out var parent);
|
||||
|
||||
var afe = new ArchiveFileEntry(Path.GetFileName(f), true, parent);
|
||||
|
||||
_fileEntries.Add(f, afe);
|
||||
});
|
||||
|
||||
// add the last path fragments, which is a file
|
||||
if (!entry.IsDirectory)
|
||||
{
|
||||
var file = pf.Last();
|
||||
|
||||
_fileEntries.TryGetValue(GetDirectoryName(file), out var parent);
|
||||
|
||||
_fileEntries.Add(file, new ArchiveFileEntry(Path.GetFileName(entry.Key), false, parent)
|
||||
{
|
||||
Encrypted = entry.IsEncrypted,
|
||||
Size = (ulong)entry.Size,
|
||||
ModifiedDate = entry.LastModifiedTime ?? new DateTime()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private string GetDirectoryName(string path)
|
||||
{
|
||||
var d = Path.GetDirectoryName(path);
|
||||
|
||||
return d ?? string.Empty;
|
||||
}
|
||||
|
||||
private string[] GetPathFragments(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
return [];
|
||||
|
||||
var frags = path.Split('\\', '/').Where(f => !string.IsNullOrEmpty(f)).ToArray();
|
||||
|
||||
return [.. frags.Select((s, i) => frags.Take(i + 1).Aggregate((a, b) => a + "\\" + b))];
|
||||
}
|
||||
|
||||
[NotifyPropertyChangedInvocator]
|
||||
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// 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 PureSharpCompress.Common;
|
||||
using PureSharpCompress.Readers;
|
||||
using System;
|
||||
using System.Text;
|
||||
using UtfUnknown;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer.ArchiveFile;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// 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 QuickLook.Common.ExtensionMethods;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer.ArchiveFile;
|
||||
|
||||
public sealed class Percent100ToVisibilityVisibleConverter : DependencyObject, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
value ??= 0;
|
||||
|
||||
var percent = (double)value;
|
||||
return Math.Abs(percent - 100) < 0.00001 ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Percent100ToVisibilityCollapsedConverter : DependencyObject, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
value ??= 0;
|
||||
|
||||
var percent = (double)value;
|
||||
return Math.Abs(percent - 100) < 0.00001 ? Visibility.Collapsed : Visibility.Visible;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LevelToIndentConverter : DependencyObject, IMultiValueConverter
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LevelToBooleanConverter : DependencyObject, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var level = (int)value;
|
||||
|
||||
return level < 2;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class BooleanToAsteriskConverter : DependencyObject, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var b = (bool)value;
|
||||
|
||||
return b ? "*" : string.Empty;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SizePrettyPrintConverter : DependencyObject, IMultiValueConverter
|
||||
{
|
||||
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var size = (ulong)values[0];
|
||||
var isFolder = (bool)values[1];
|
||||
|
||||
return isFolder ? string.Empty : ((long)size).ToPrettySize(2);
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DatePrintConverter : DependencyObject, IMultiValueConverter
|
||||
{
|
||||
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var date = (DateTime)values[0];
|
||||
var isFolder = (bool)values[1];
|
||||
|
||||
return isFolder ? string.Empty : date.ToString(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class FileExtToIconConverter : DependencyObject, IMultiValueConverter
|
||||
{
|
||||
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var name = (string)values[0];
|
||||
var isFolder = (bool)values[1];
|
||||
|
||||
if (isFolder)
|
||||
return IconManager.FindIconForDir(false);
|
||||
|
||||
return IconManager.FindIconForFilename(name, false);
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// 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.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer.ArchiveFile;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
|
||||
{
|
||||
foreach (var item in enumeration)
|
||||
action(item);
|
||||
}
|
||||
|
||||
public static T GetDescendantByType<T>(this Visual element) where T : class
|
||||
{
|
||||
if (element == null)
|
||||
return default;
|
||||
if (element.GetType() == typeof(T))
|
||||
return element as T;
|
||||
|
||||
T foundElement = null;
|
||||
(element as FrameworkElement)?.ApplyTemplate();
|
||||
|
||||
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
|
||||
{
|
||||
var visual = VisualTreeHelper.GetChild(element, i) as Visual;
|
||||
foundElement = visual.GetDescendantByType<T>();
|
||||
if (foundElement != null)
|
||||
break;
|
||||
}
|
||||
|
||||
return foundElement;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// 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.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer.ArchiveFile;
|
||||
|
||||
/// <summary>
|
||||
/// Internals are mostly from here:
|
||||
/// http://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using
|
||||
/// Caches all results.
|
||||
/// </summary>
|
||||
public static class IconManager
|
||||
{
|
||||
private static ImageSource SmallDirIcon;
|
||||
private static ImageSource LargeDirIcon;
|
||||
private static readonly Dictionary<string, ImageSource> SmallIconCache = [];
|
||||
private static readonly Dictionary<string, ImageSource> LargeIconCache = [];
|
||||
|
||||
public static void ClearCache()
|
||||
{
|
||||
SmallDirIcon = LargeDirIcon = null;
|
||||
|
||||
SmallIconCache.Clear();
|
||||
LargeIconCache.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the icon of a directory
|
||||
/// </summary>
|
||||
/// <param name="large">16x16 or 32x32 icon</param>
|
||||
/// <returns>an icon</returns>
|
||||
public static ImageSource FindIconForDir(bool large)
|
||||
{
|
||||
var icon = large ? LargeDirIcon : SmallDirIcon;
|
||||
if (icon != null)
|
||||
return icon;
|
||||
icon = IconReader.GetFolderIcon(large ? IconReader.IconSize.Large : IconReader.IconSize.Small,
|
||||
false)
|
||||
.ToImageSource();
|
||||
if (large)
|
||||
LargeDirIcon = icon;
|
||||
else
|
||||
SmallDirIcon = icon;
|
||||
return icon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an icon for a given filename
|
||||
/// </summary>
|
||||
/// <param name="fileName">any filename</param>
|
||||
/// <param name="large">16x16 or 32x32 icon</param>
|
||||
/// <returns>null if path is null, otherwise - an icon</returns>
|
||||
public static ImageSource FindIconForFilename(string fileName, bool large)
|
||||
{
|
||||
var extension = Path.GetExtension(fileName);
|
||||
if (extension == null)
|
||||
return null;
|
||||
var cache = large ? LargeIconCache : SmallIconCache;
|
||||
if (cache.TryGetValue(extension, out ImageSource icon))
|
||||
return icon;
|
||||
icon = IconReader.GetFileIcon(fileName, large ? IconReader.IconSize.Large : IconReader.IconSize.Small,
|
||||
false)
|
||||
.ToImageSource();
|
||||
cache.Add(extension, icon);
|
||||
return icon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// http://stackoverflow.com/a/6580799/1943849
|
||||
/// </summary>
|
||||
private static ImageSource ToImageSource(this Icon icon)
|
||||
{
|
||||
var imageSource = Imaging.CreateBitmapSourceFromHIcon(
|
||||
icon.Handle,
|
||||
Int32Rect.Empty,
|
||||
BitmapSizeOptions.FromEmptyOptions());
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides static methods to read system icons for both folders and files.
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <code>IconReader.GetFileIcon("c:\\general.xls");</code>
|
||||
/// </example>
|
||||
private static class IconReader
|
||||
{
|
||||
/// <summary>
|
||||
/// Options to specify the size of icons to return.
|
||||
/// </summary>
|
||||
public enum IconSize
|
||||
{
|
||||
/// <summary>
|
||||
/// Specify large icon - 32 pixels by 32 pixels.
|
||||
/// </summary>
|
||||
Large = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Specify small icon - 16 pixels by 16 pixels.
|
||||
/// </summary>
|
||||
Small = 1
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the icon of a folder.
|
||||
/// </summary>
|
||||
/// <param name="size">Large or small</param>
|
||||
/// <param name="linkOverlay">Whether to include the link icon</param>
|
||||
/// <returns>System.Drawing.Icon</returns>
|
||||
public static Icon GetFolderIcon(IconSize size, bool linkOverlay)
|
||||
{
|
||||
var shfi = new Shell32.Shfileinfo();
|
||||
var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;
|
||||
if (linkOverlay) flags += Shell32.ShgfiLinkoverlay;
|
||||
/* Check the size specified for return. */
|
||||
if (IconSize.Small == size)
|
||||
flags += Shell32.ShgfiSmallicon;
|
||||
else
|
||||
flags += Shell32.ShgfiLargeicon;
|
||||
Shell32.SHGetFileInfo("placeholder",
|
||||
Shell32.FileAttributeDirectory,
|
||||
ref shfi,
|
||||
(uint)Marshal.SizeOf(shfi),
|
||||
flags);
|
||||
// Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
|
||||
var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
|
||||
User32.DestroyIcon(shfi.hIcon); // Cleanup
|
||||
return icon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an icon for a given file - indicated by the name parameter.
|
||||
/// </summary>
|
||||
/// <param name="name">Pathname for file.</param>
|
||||
/// <param name="size">Large or small</param>
|
||||
/// <param name="linkOverlay">Whether to include the link icon</param>
|
||||
/// <returns>System.Drawing.Icon</returns>
|
||||
public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
|
||||
{
|
||||
var shfi = new Shell32.Shfileinfo();
|
||||
var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;
|
||||
if (linkOverlay) flags += Shell32.ShgfiLinkoverlay;
|
||||
/* Check the size specified for return. */
|
||||
if (IconSize.Small == size)
|
||||
flags += Shell32.ShgfiSmallicon;
|
||||
else
|
||||
flags += Shell32.ShgfiLargeicon;
|
||||
Shell32.SHGetFileInfo(name,
|
||||
Shell32.FileAttributeNormal,
|
||||
ref shfi,
|
||||
(uint)Marshal.SizeOf(shfi),
|
||||
flags);
|
||||
// Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
|
||||
var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
|
||||
User32.DestroyIcon(shfi.hIcon); // Cleanup
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps necessary Shell32.dll structures and functions required to retrieve Icon Handles using SHGetFileInfo. Code
|
||||
/// courtesy of MSDN Cold Rooster Consulting case study.
|
||||
/// </summary>
|
||||
private static class Shell32
|
||||
{
|
||||
private const int MaxPath = 256;
|
||||
public const uint ShgfiIcon = 0x000000100; // get icon
|
||||
public const uint ShgfiLinkoverlay = 0x000008000; // put a link overlay on icon
|
||||
public const uint ShgfiLargeicon = 0x000000000; // get large icon
|
||||
public const uint ShgfiSmallicon = 0x000000001; // get small icon
|
||||
public const uint ShgfiUsefileattributes = 0x000000010; // use passed dwFileAttribute
|
||||
public const uint FileAttributeNormal = 0x00000080;
|
||||
public const uint FileAttributeDirectory = 0x00000010;
|
||||
|
||||
[DllImport("Shell32.dll")]
|
||||
public static extern nint SHGetFileInfo(
|
||||
string pszPath,
|
||||
uint dwFileAttributes,
|
||||
ref Shfileinfo psfi,
|
||||
uint cbFileInfo,
|
||||
uint uFlags
|
||||
);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Shfileinfo
|
||||
{
|
||||
private const int Namesize = 80;
|
||||
public readonly nint hIcon;
|
||||
private readonly int iIcon;
|
||||
private readonly uint dwAttributes;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MaxPath)]
|
||||
private readonly string szDisplayName;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = Namesize)]
|
||||
private readonly string szTypeName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps necessary functions imported from User32.dll. Code courtesy of MSDN Cold Rooster Consulting example.
|
||||
/// </summary>
|
||||
private static class User32
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides access to function required to delete handle. This method is used internally
|
||||
/// and is not required to be called separately.
|
||||
/// </summary>
|
||||
/// <param name="hIcon">Pointer to icon handle.</param>
|
||||
/// <returns>N/A</returns>
|
||||
[DllImport("User32.dll")]
|
||||
public static extern int DestroyIcon(nint hIcon);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user