using System; using System.Collections.Generic; using System.IO; using System.Linq; using BizHawk.Common; using SharpCompress.Archives; using SharpCompress.Common; namespace BizHawk.Client.Common { /// /// An ArchiveHandler implemented using SharpCompress from NuGet /// /// /// Intended for Unix, which can't use SevenZipSharp, but later we might sacrifice whatever speed advantage that library has for the lower workload of one cross-platform library. /// /// public class SharpCompressArchiveHandler : IHawkFileArchiveHandler { private IArchive _archive; public void Dispose() { _archive?.Dispose(); _archive = null; } public bool CheckSignature(string fileName, out int offset, out bool isExecutable) { offset = 0; isExecutable = false; try { using (var arcTest = ArchiveFactory.Open(fileName)) switch (arcTest.Type) { case ArchiveType.Zip: case ArchiveType.SevenZip: return true; } } catch (Exception _) { // ignored } return false; } public IHawkFileArchiveHandler Construct(string path) { var ret = new SharpCompressArchiveHandler(); ret.Open(path); return ret; } private void Open(string path) => _archive = ArchiveFactory.Open(path); public List Scan() => _archive.Entries.Where(e => !e.IsDirectory) .Select((e, i) => new HawkFileArchiveItem { Name = HawkFile.Util_FixArchiveFilename(e.Key), Size = e.Size, Index = i, ArchiveIndex = i }) .ToList(); public void ExtractFile(int index, Stream stream) { using (var entryStream = _archive.Entries.Where(e => !e.IsDirectory).ElementAt(index).OpenEntryStream()) entryStream.CopyTo(stream); } } }