using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.ObjectModel; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; namespace Sarand.Net.Standard.Data.Zip { //======================================================================= public class SaMsZipArchive : SaErrorStateItem { //------------------------------------------------------------------- // Data members. private ZipArchive zipArchive; //------------------------------------------------------------------- // Construction. public SaMsZipArchive () { } protected override void Dispose (bool disposing) { if (disposing) { if (zipArchive != null) zipArchive.Dispose(); } } //------------------------------------------------------------------- // Properties. //------------------------------------------------------------------- // A r c h i v e F i l e //------------------------------------------------------------------- public bool OpenArchive (String filePath, ZipArchiveMode mode = ZipArchiveMode.Read) { ClearErrorState(); if (StringUtil.IsEmpty(filePath)) return SetErrorState("Filename must be supplied"); CloseArchive(); try { zipArchive = ZipFile.OpenRead(filePath);//, mode); ReadOnlyCollection entries = zipArchive.Entries; int entryCount = entries.Count; } catch (Exception exc) { SaLogger.Warn(GetType().Name + ".OpenArchive " + ExceptionUtil.GetLogMessage(exc)); return SetErrorState(ExceptionUtil.GetErrorStateMessage(exc)); } return !HasErrorState(); } /// Open ZIP archive from data stream. public bool OpenArchive (Stream stream, ZipArchiveMode mode = ZipArchiveMode.Read, bool leaveOpen = true, Encoding entryNameEncoding = null) { ClearErrorState(); if (entryNameEncoding == null) entryNameEncoding = Encoding.Default; CloseArchive(); try { zipArchive = new ZipArchive(stream, mode, leaveOpen, entryNameEncoding); ReadOnlyCollection entries = zipArchive.Entries; int entryCount = entries.Count; } catch (Exception exc) { SaLogger.Warn(GetType().Name + ".OpenArchive " + ExceptionUtil.GetLogMessage(exc)); return SetErrorState(ExceptionUtil.GetErrorStateMessage(exc)); } return !HasErrorState(); } public bool IsArchiveOpen () { return (zipArchive != null); } /// Close eth ZIP archive. public bool CloseArchive () { ClearErrorState(); try { if (zipArchive != null) zipArchive.Dispose(); } catch (Exception exc) { SetErrorState(ExceptionUtil.GetErrorStateMessage(exc)); SaLogger.Warn(GetType().Name + ".CloseArchive " + ExceptionUtil.GetLogMessage(exc)); } zipArchive = null; return !HasErrorState(); } //------------------------------------------------------------------- // F i l e s //------------------------------------------------------------------- public MemoryStream ExtractFileToStream (String filename) { if (!IsArchiveOpen()) return null; foreach (ZipArchiveEntry entry in zipArchive.Entries) { //SaLogger.Debug(GetType().Name + ".GetFile Entry: " + entry.FullName); if (entry.FullName.EqualsCI(filename) || entry.Name.EqualsCI(filename)) { using (Stream entryStream = entry.Open()) { MemoryStream stream = new MemoryStream(); entryStream.CopyTo(stream); return stream; } } } return null; } //------------------------------------------------------------------- } //======================================================================= }