Tried SharpCompress but had 20x speed decrement. Not using.

This commit is contained in:
Joseph Milazzo 2021-01-25 18:18:07 -06:00
parent e7f2baaa94
commit 6cc11d5c4c
5 changed files with 85 additions and 18 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
@ -41,10 +42,10 @@ namespace API.Services
return null;
}
Volume volume = await _unitOfWork.SeriesRepository.GetVolumeAsync(volumeId);
foreach (var file in volume.Files)
{
var extractPath = GetVolumeCachePath(volumeId, file);
ExtractArchive(file.FilePath, extractPath);
}
@ -91,6 +92,74 @@ namespace API.Services
_logger.LogInformation("Cache directory purged");
}
// private void ExtractArchive(string archivePath, string extractPath, int expectedPages)
// {
// if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
// {
// _logger.LogError($"Archive {archivePath} could not be found.");
// }
//
// var extractDirectoryInfo = new DirectoryInfo(extractPath);
//
// // If the extraction and flattening aren't perfect, adding extra cases adds a serious amount of extra time.
// if (Directory.Exists(extractPath)) // && extractDirectoryInfo.EnumerateFiles().Count() >= expectedPages
// {
// _logger.LogDebug($"Archive {archivePath} has already been extracted. Returning existing folder.");
// return;
// }
//
// Stopwatch sw = Stopwatch.StartNew();
// var needsFlattening = false;
// var options = new ExtractionOptions
// {
// ExtractFullPath = true,
// Overwrite = true
// };
//
// using Stream stream = File.OpenRead(archivePath);
// using var reader = ReaderFactory.Open(stream);
// while (reader.MoveToNextEntry())
// {
// if (!reader.Entry.IsDirectory)
// {
// try
// {
// reader.WriteEntryToDirectory(extractPath, options);
// }
// catch (Exception e)
// {
// _logger.LogError(e, "There was an issue with extracting image.");
// }
// }
//
// if (!needsFlattening)
// {
// needsFlattening = reader.Entry.IsDirectory;
// }
// }
//
//
// _logger.LogDebug($"Extracted archive to {extractPath} in {sw.ElapsedMilliseconds} milliseconds.");
//
//
// if (needsFlattening)
// {
// sw = Stopwatch.StartNew();
// _logger.LogInformation("Extracted archive is nested in root folder, flattening...");
// try
// {
// extractDirectoryInfo.Flatten();
// }
// catch (Exception ex)
// {
// _logger.LogError(ex, "There was an issue extracting archive.");
// return;
// }
// _logger.LogInformation($"Flattened in {sw.ElapsedMilliseconds} milliseconds");
// }
// }
/// <summary>
/// Extracts an archive to a temp cache directory. Returns path to new directory. If temp cache directory already exists,
/// will return that without performing an extraction. Returns empty string if there are any invalidations which would
@ -99,35 +168,34 @@ namespace API.Services
/// <param name="archivePath">A valid file to an archive file.</param>
/// <param name="extractPath">Path to extract to</param>
/// <returns></returns>
private string ExtractArchive(string archivePath, string extractPath)
private void ExtractArchive(string archivePath, string extractPath)
{
// NOTE: This is used by Cache Service
if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
{
_logger.LogError($"Archive {archivePath} could not be found.");
return "";
}
if (Directory.Exists(extractPath))
{
_logger.LogDebug($"Archive {archivePath} has already been extracted. Returning existing folder.");
return extractPath;
}
Stopwatch sw = Stopwatch.StartNew();
using ZipArchive archive = ZipFile.OpenRead(archivePath);
// TODO: Throw error if we couldn't extract
var needsFlattening = archive.Entries.Count > 0 && !Path.HasExtension(archive.Entries.ElementAt(0).FullName);
if (!archive.HasFiles() && !needsFlattening) return "";
if (!archive.HasFiles() && !needsFlattening) return;
archive.ExtractToDirectory(extractPath);
_logger.LogDebug($"Extracting archive to {extractPath}");
_logger.LogDebug($"[OLD] Extracted archive to {extractPath} in {sw.ElapsedMilliseconds} milliseconds.");
if (!needsFlattening) return extractPath;
_logger.LogInformation("Extracted archive is nested in root folder, flattening...");
new DirectoryInfo(extractPath).Flatten();
return extractPath;
if (needsFlattening)
{
sw = Stopwatch.StartNew();
_logger.LogInformation("Extracted archive is nested in root folder, flattening...");
new DirectoryInfo(extractPath).Flatten();
_logger.LogInformation($"[OLD] Flattened in {sw.ElapsedMilliseconds} milliseconds");
}
}