Skip to content

Commit

Permalink
Replace DotNetZip with Sharpcompress
Browse files Browse the repository at this point in the history
  • Loading branch information
DSPaul committed Apr 20, 2024
1 parent 492e9e0 commit 4e58471
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 77 deletions.
8 changes: 4 additions & 4 deletions src/COMPASS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@
<PackageReference Include="Autofac" Version="8.0.0" />
<PackageReference Include="Autoupdater.NET.Official" Version="1.8.5" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="DotNetZip" Version="1.16.0" />
<PackageReference Include="FuzzySharp" Version="2.0.2" />
<PackageReference Include="gong-wpf-dragdrop" Version="3.2.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.59" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.60" />
<PackageReference Include="itext7" Verion="8.0.3" Version="8.0.3" />
<PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.3" />
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="log4net" Version="2.0.17" />
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.6.0" />
<PackageReference Include="Magick.NET.SystemDrawing" Version="7.2.0" />
<PackageReference Include="MaterialDesignThemes" Version="4.9.0" />
Expand All @@ -77,7 +76,8 @@
<PackageReference Include="OpenCvSharp4.Extensions" Version="4.9.0.20240103" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.9.0.20240103" />
<PackageReference Include="ScrapySharp" Version="3.0.0" />
<PackageReference Include="Selenium.WebDriver" Verion="4.17.0" Version="4.18.1" />
<PackageReference Include="Selenium.WebDriver" Verion="4.17.0" Version="4.19.0" />
<PackageReference Include="SharpCompress" Version="0.36.0" />
<PackageReference Include="VirtualizingWrapPanel" Version="2.0.5" />
<PackageReference Include="WpfAnimatedGif" Version="2.0.2" />
<PackageReference Include="ZXing.Net" Version="0.16.9" />
Expand Down
16 changes: 9 additions & 7 deletions src/Services/IOService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using COMPASS.Tools;
using COMPASS.ViewModels;
using HtmlAgilityPack;
using Ionic.Zip;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;
using Ookii.Dialogs.Wpf;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -135,7 +136,7 @@ public static async Task<string> UnZipCollection(string path)
ClearTmpData(tmpCollectionPath);

//unzip the file to tmp folder
using ZipFile zip = ZipFile.Read(path);
using ZipArchive archive = ZipArchive.Open(path);

//report progress
var progressVM = ProgressViewModel.GetInstance();
Expand All @@ -144,11 +145,12 @@ public static async Task<string> UnZipCollection(string path)
progressVM.TotalAmount = 1;

//extract
await Task.Run(() => zip.ExtractAll(tmpCollectionPath));
await Task.Run(() => archive.ExtractToDirectory(tmpCollectionPath)); //TODO add cancelation and progress reporting

progressVM.IncrementCounter();
return tmpCollectionPath;
}

public static void ClearTmpData(string? tempPath = null)
{
if (tempPath == null)
Expand Down Expand Up @@ -283,9 +285,9 @@ public static string[] PickFolders()
}

//Check compatibility
using (ZipFile zip = ZipFile.Read(path))
using (ZipArchive archive = ZipArchive.Open(path))
{
var satchelInfoFile = zip.SingleOrDefault(entry => entry.FileName == Constants.SatchelInfoFileName);
var satchelInfoFile = archive.Entries.SingleOrDefault(entry => entry.Key == Constants.SatchelInfoFileName);
if (satchelInfoFile == null)
{
//No version information means we cannot ensure compatibility, so abort
Expand All @@ -298,7 +300,7 @@ public static string[] PickFolders()

//Read the file contents
using var stream = new MemoryStream();
satchelInfoFile.Extract(stream);
satchelInfoFile.WriteTo(stream);
stream.Seek(0, SeekOrigin.Begin);
using StreamReader reader = new(stream);
string json = reader.ReadToEnd();
Expand All @@ -317,7 +319,7 @@ public static string[] PickFolders()
var currentVersion = Assembly.GetExecutingAssembly().GetName().Version!;
var minVersions = new List<Version> { currentVersion }; //keep a list of min requirements

var filesInZip = zip.Select(entry => entry.FileName).ToList();
var filesInZip = archive.Entries.Select(entry => entry.Key).ToList();

//Check Codex version
if (filesInZip.Contains(Constants.CodicesFileName))
Expand Down
10 changes: 6 additions & 4 deletions src/ViewModels/CollectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using COMPASS.Tools;
using COMPASS.ViewModels.Import;
using COMPASS.Windows;
using Ionic.Zip;
using Microsoft.Win32;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
Expand Down Expand Up @@ -376,11 +378,11 @@ public void ExportTags()
CurrentCollection.SaveTags();

string targetPath = saveFileDialog.FileName;
using ZipFile zip = new();
zip.AddFile(CurrentCollection.TagsDataFilePath, "");
using var archive = ZipArchive.Create();
archive.AddEntry(Constants.TagsFileName, CurrentCollection.TagsDataFilePath);

//Export
zip.Save(targetPath);
archive.SaveTo(targetPath, CompressionType.None);
Logger.Info($"Exported Tags from {CurrentCollection.DirectoryName} to {targetPath}");
}

Expand Down
38 changes: 17 additions & 21 deletions src/ViewModels/ExportCollectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using COMPASS.Models;
using COMPASS.Services;
using COMPASS.Tools;
using Ionic.Zip;
using Microsoft.Win32;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using System;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -128,8 +130,7 @@ public async Task ExportToFile(string targetPath)
ContentSelectorVM.CuratedCollection.InitAsNew();
ContentSelectorVM.CuratedCollection.Save();

using ZipFile zip = new();
zip.AddDirectory(ContentSelectorVM.CuratedCollection.FullDataPath);
using var archive = ZipArchive.Create();

//Change Codex Path to relative and add those files if the options is set
var itemsWithOfflineSource = ContentSelectorVM.CuratedCollection.AllCodices
Expand All @@ -143,8 +144,7 @@ public async Task ExportToFile(string targetPath)
//Add the file
if (IncludeFiles && File.Exists(codex.Path))
{
int indexStartFilename = relativePath.Length - Path.GetFileName(codex.Path)!.Length;
zip.AddFile(codex.Path, Path.Combine("Files", relativePath[0..indexStartFilename]));
archive.AddEntry(Path.Combine("Files", relativePath), codex.Path);
//keep the relative path, will be used during import to link the included files
codex.Path = relativePath;
}
Expand All @@ -158,36 +158,28 @@ public async Task ExportToFile(string targetPath)
//Add cover art
if (IncludeCoverArt && File.Exists(codex.CoverArt))
{
zip.AddFile(codex.CoverArt, "CoverArt");
archive.AddEntry(Path.Combine("CoverArt", Path.GetFileName(codex.CoverArt)), codex.CoverArt);
}
}

//Save changes
ContentSelectorVM.CuratedCollection.SaveCodices();
zip.UpdateFile(ContentSelectorVM.CuratedCollection.CodicesDataFilePath, "");

//Now add xml files
archive.AddAllFromDirectory(ContentSelectorVM.CuratedCollection.FullDataPath);

//Progress reporting
progressVM.Text = "Exporting Collection";
progressVM.ShowCount = false;
progressVM.ResetCounter();
zip.SaveProgress += (_, args) =>
{
progressVM.TotalAmount = Math.Max(progressVM.TotalAmount, args.EntriesTotal);
if (args.EventType == ZipProgressEventType.Saving_AfterWriteEntry)
{
progressVM.IncrementCounter();
}
};
//TODO find new way to track progress

//Add version so we can check compatibility when importing
SatchelInfo info = new();
zip.AddEntry(Constants.SatchelInfoFileName, JsonSerializer.Serialize(info));
archive.AddEntry(Constants.SatchelInfoFileName, new MemoryStream(JsonSerializer.SerializeToUtf8Bytes(info)));

//Export
await Task.Run(() =>
{
zip.Save(targetPath);
Directory.Delete(ContentSelectorVM.CuratedCollection.FullDataPath, true);
});
await Task.Run(() => archive.SaveTo(targetPath, CompressionType.None));
Logger.Info($"Exported {CollectionToExport.DirectoryName} to {targetPath}");
}
catch (Exception ex)
Expand All @@ -196,6 +188,10 @@ await Task.Run(() =>
progressVM.Clear();
CloseAction?.Invoke();
}
finally
{
Directory.Delete(ContentSelectorVM.CuratedCollection.FullDataPath, true);
}
}

public void UpdateSteps()
Expand Down
63 changes: 22 additions & 41 deletions src/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
using COMPASS.Tools;
using COMPASS.ViewModels.Import;
using COMPASS.Windows;
using Ionic.Zip;
using Microsoft.Win32;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -689,13 +690,11 @@ public void BrowseLocalFiles()
Process.Start(startInfo);
}

private readonly BackgroundWorker _createZipWorker = new();
private readonly BackgroundWorker _extractZipWorker = new();
private LoadingWindow? _lw;

private RelayCommand? _backupLocalFilesCommand;
public RelayCommand BackupLocalFilesCommand => _backupLocalFilesCommand ??= new(BackupLocalFiles);
public void BackupLocalFiles()
private AsyncRelayCommand? _backupLocalFilesCommand;
public AsyncRelayCommand BackupLocalFilesCommand => _backupLocalFilesCommand ??= new(BackupLocalFiles);
public async Task BackupLocalFiles()
{
SaveFileDialog saveFileDialog = new()
{
Expand All @@ -712,16 +711,19 @@ public void BackupLocalFiles()
MainViewModel.CollectionVM.CurrentCollection.Save();
SavePreferences();

_createZipWorker.DoWork += CreateZip;
_createZipWorker.RunWorkerCompleted += CreateZipDone;
await Task.Run(() => CompressUserDataToZip(targetPath));

_createZipWorker.RunWorkerAsync(targetPath);
_lw.Close();
}
}

private RelayCommand? _restoreBackupCommand;
public RelayCommand RestoreBackupCommand => _restoreBackupCommand ??= new(RestoreBackup);
public void RestoreBackup()
private void CompressUserDataToZip(string zipPath) =>
//zip up collections, easiest with system.IO.Compression
System.IO.Compression.ZipFile.CreateFromDirectory(CodexCollection.CollectionsPath, zipPath, System.IO.Compression.CompressionLevel.Optimal, true);

private AsyncRelayCommand? _restoreBackupCommand;
public AsyncRelayCommand RestoreBackupCommand => _restoreBackupCommand ??= new(RestoreBackup);
public async Task RestoreBackup()
{
OpenFileDialog openFileDialog = new()
{
Expand All @@ -734,44 +736,23 @@ public void RestoreBackup()
_lw = new("Restoring Backup");
_lw.Show();

_extractZipWorker.DoWork += ExtractZip;
_extractZipWorker.RunWorkerCompleted += ExtractZipDone;

_extractZipWorker.RunWorkerAsync(targetPath);
}
}
await Task.Run(() => ExtractZip(targetPath));

private void CreateZip(object? sender, DoWorkEventArgs e)
{
if (e.Argument is not string targetPath)
{
return;
//restore collection that was open
MainViewModel.CollectionVM.CurrentCollection = new(Properties.Settings.Default.StartupCollection);
_lw?.Close();
}

using ZipFile zip = new();
zip.AddDirectory(CodexCollection.CollectionsPath, "Collections");
zip.AddFile(PreferencesFilePath, "");
zip.Save(targetPath);
}

private void ExtractZip(object? sender, DoWorkEventArgs e)
private void ExtractZip(string sourcePath)
{
if (e.Argument is not string sourcePath || !Path.Exists(sourcePath))
if (!Path.Exists(sourcePath))
{
Logger.Warn($"Cannot extract sourcePath as it does not exit");
return;
}
using ZipFile zip = ZipFile.Read(sourcePath);
zip.ExtractAll(CompassDataPath, ExtractExistingFileAction.OverwriteSilently);
}

private void CreateZipDone(object? sender, RunWorkerCompletedEventArgs e) => _lw?.Close();

private void ExtractZipDone(object? sender, RunWorkerCompletedEventArgs e)
{
//restore collection that was open
MainViewModel.CollectionVM.CurrentCollection = new(Properties.Settings.Default.StartupCollection);
_lw?.Close();
using ZipArchive archive = ZipArchive.Open(sourcePath);
archive.ExtractToDirectory(CompassDataPath);
}
#endregion

Expand Down
7 changes: 7 additions & 0 deletions src/Windows/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,13 @@
ScrapySharp
</Hyperlink>
</TextBlock>-->
<TextBlock Text="Zipping and unzipping:"/>
<TextBlock>
<Hyperlink NavigateUri="https://github.com/adamhathcock/sharpcompress"
RequestNavigate="Hyperlink_RequestNavigate" TextDecorations="None">
SharpCompress
</Hyperlink>
</TextBlock>
<TextBlock Text="Website Screenshotting:"/>
<TextBlock>
<Hyperlink NavigateUri="https://github.com/SeleniumHQ/selenium"
Expand Down

0 comments on commit 4e58471

Please sign in to comment.