124 lines
3.7 KiB
C#
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($"{SysUtil.Fn} {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($"{SysUtil.Fn} {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($"{SysUtil.Fn} {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($"{SysUtil.Fn} 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;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|