Files
Common-VS2019/SacsStandardProc/Proc/Zip/SaMsZipArchive.cs
T
2021-07-08 20:51:25 +01:00

124 lines
3.7 KiB
C#

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.Proc.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<ZipArchiveEntry> 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();
}
/// <summary>Open ZIP archive from data stream.</summary>
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<ZipArchiveEntry> 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);
}
/// <summary>Close eth ZIP archive.</summary>
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;
}
//-------------------------------------------------------------------
}
//=======================================================================
}