forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecompressArchive.cs
116 lines (90 loc) · 3.87 KB
/
DecompressArchive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Files Community
// Licensed under the MIT License.
using Files.App.Dialogs;
using Files.Shared.Helpers;
using Microsoft.UI.Xaml.Controls;
using System.IO;
using System.Text;
using Windows.Foundation.Metadata;
using Windows.Storage;
namespace Files.App.Actions
{
internal sealed partial class DecompressArchive : BaseDecompressArchiveAction
{
public override string Label
=> Strings.ExtractFiles.GetLocalizedResource();
public override string Description
=> Strings.DecompressArchiveDescription.GetLocalizedResource();
public override HotKey HotKey
=> new(Keys.E, KeyModifiers.Ctrl);
public DecompressArchive()
{
}
public override async Task ExecuteAsync(object? parameter = null)
{
if (context.ShellPage is null)
return;
var archivePath = GetArchivePath();
if (string.IsNullOrEmpty(archivePath))
return;
BaseStorageFile archive = await StorageHelpers.ToStorageItem<BaseStorageFile>(archivePath);
if (archive?.Path is null)
return;
var isArchiveEncrypted = await FilesystemTasks.Wrap(() => StorageArchiveService.IsEncryptedAsync(archive.Path));
var isArchiveEncodingUndetermined = await FilesystemTasks.Wrap(() => StorageArchiveService.IsEncodingUndeterminedAsync(archive.Path));
var password = string.Empty;
Encoding? encoding = null;
DecompressArchiveDialog decompressArchiveDialog = new();
DecompressArchiveDialogViewModel decompressArchiveViewModel = new(archive)
{
IsArchiveEncrypted = isArchiveEncrypted,
IsArchiveEncodingUndetermined = isArchiveEncodingUndetermined,
ShowPathSelection = true
};
decompressArchiveDialog.ViewModel = decompressArchiveViewModel;
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
decompressArchiveDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
ContentDialogResult option = await decompressArchiveDialog.TryShowAsync();
if (option != ContentDialogResult.Primary)
return;
if (isArchiveEncrypted && decompressArchiveViewModel.Password is not null)
password = Encoding.UTF8.GetString(decompressArchiveViewModel.Password);
encoding = decompressArchiveViewModel.SelectedEncoding.Encoding;
// Check if archive still exists
if (!StorageHelpers.Exists(archive.Path))
return;
BaseStorageFolder destinationFolder = decompressArchiveViewModel.DestinationFolder;
string destinationFolderPath = decompressArchiveViewModel.DestinationFolderPath;
if (destinationFolder is null)
{
BaseStorageFolder parentFolder = await StorageHelpers.ToStorageItem<BaseStorageFolder>(Path.GetDirectoryName(archive.Path) ?? string.Empty);
destinationFolder = await FilesystemTasks.Wrap(() => parentFolder.CreateFolderAsync(Path.GetFileName(destinationFolderPath), CreationCollisionOption.GenerateUniqueName).AsTask());
}
// Operate decompress
var result = await FilesystemTasks.Wrap(() =>
StorageArchiveService.DecompressAsync(archive?.Path ?? string.Empty, destinationFolder?.Path ?? string.Empty, password, encoding));
if (decompressArchiveViewModel.OpenDestinationFolderOnCompletion)
await NavigationHelpers.OpenPath(destinationFolderPath, context.ShellPage, FilesystemItemType.Directory);
}
protected override bool CanDecompressInsideArchive()
{
return
context.PageType == ContentPageTypes.ZipFolder &&
!context.HasSelection &&
context.Folder is not null &&
FileExtensionHelpers.IsZipFile(Path.GetExtension(context.Folder.ItemPath));
}
protected override bool CanDecompressSelectedItems()
{
return context.SelectedItems.Count == 1 && base.CanDecompressSelectedItems();
}
private string? GetArchivePath()
{
if (!string.IsNullOrEmpty(context.SelectedItem?.ItemPath))
return context.SelectedItem?.ItemPath;
if (context.PageType == ContentPageTypes.ZipFolder && !context.HasSelection)
return context.Folder?.ItemPath;
return null;
}
}
}