-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #523 from paillave/dotnet9-migration
feat: implement zip and unzip functionality with new classes and para…
- Loading branch information
Showing
4 changed files
with
139 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,58 @@ | ||
// using System; | ||
// using System.Linq; | ||
// using System.IO; | ||
// using ICSharpCode.SharpZipLib.Zip; | ||
// using Paillave.Etl.Core; | ||
// using System.Threading; | ||
// using Microsoft.Extensions.FileSystemGlobbing; | ||
using System; | ||
using System.Linq; | ||
using System.IO; | ||
using ICSharpCode.SharpZipLib.Zip; | ||
using Paillave.Etl.Core; | ||
using System.Threading; | ||
using Microsoft.Extensions.FileSystemGlobbing; | ||
using System.Collections.Generic; | ||
|
||
// namespace Paillave.Etl.Zip | ||
// { | ||
// public class ZipFileProcessorValuesProvider : ValuesProviderBase<IFileValue, IFileValue> | ||
// { | ||
// private ZipFileProcessorParams _args; | ||
// public ZipFileProcessorValuesProvider(UnzipFileProcessorParams args) | ||
// => _args = args; | ||
// public override ProcessImpact PerformanceImpact => ProcessImpact.Average; | ||
// public override ProcessImpact MemoryFootPrint => ProcessImpact.Average; | ||
// public override void PushValues(IFileValue input, Action<IFileValue> push, CancellationToken cancellationToken, IExecutionContext context) | ||
// { | ||
// var destinations = (input.Metadata as IFileValueWithDestinationMetadata)?.Destinations; | ||
// if (cancellationToken.IsCancellationRequested) return; | ||
// using var stream = input.Get(_args.UseStreamCopy); | ||
// using var zf = new ZipFile(stream); | ||
// var searchPattern = string.IsNullOrEmpty(_args.FileNamePattern) ? "*" : _args.FileNamePattern; | ||
// var matcher = new Matcher().AddInclude(searchPattern); | ||
namespace Paillave.Etl.Zip; | ||
|
||
// if (!String.IsNullOrEmpty(_args.Password)) | ||
// zf.Password = _args.Password; | ||
// var fileNames = zf.OfType<ZipEntry>().Where(i => i.IsFile && matcher.Match(Path.GetFileName(i.Name)).HasMatches).Select(i => i.Name).ToHashSet(); | ||
// foreach (ZipEntry zipEntry in zf) | ||
// { | ||
// if (cancellationToken.IsCancellationRequested) break; | ||
// if (zipEntry.IsFile && matcher.Match(Path.GetFileName(zipEntry.Name)).HasMatches) | ||
// { | ||
// MemoryStream outputStream = new MemoryStream(); | ||
// using (var zipStream = zf.GetInputStream(zipEntry)) | ||
// zipStream.CopyTo(outputStream, 4096); | ||
// outputStream.Seek(0, SeekOrigin.Begin); | ||
// push(new UnzippedFileValue<UnzippedFileValueMetadata>(outputStream, zipEntry.Name, new UnzippedFileValueMetadata | ||
// { | ||
// ParentFileName = input.Name, | ||
// ParentFileMetadata = input.Metadata, | ||
// Destinations = destinations, | ||
// ConnectorCode = input.Metadata.ConnectorCode, | ||
// ConnectionName = input.Metadata.ConnectionName, | ||
// ConnectorName = input.Metadata.ConnectorName | ||
// }, input, fileNames, zipEntry.Name)); | ||
// } | ||
// } | ||
// } | ||
// } | ||
// } | ||
public class ZipFileProcessorParams | ||
{ | ||
public string Password { get; set; } | ||
public bool UseStreamCopy { get; set; } = true; | ||
} | ||
public class ZippedFileValueMetadata : FileValueMetadataBase, IFileValueWithDestinationMetadata | ||
{ | ||
public IFileValueMetadata ParentFileMetadata { get; set; } | ||
public Dictionary<string, IEnumerable<Destination>> Destinations { get; set; } | ||
} | ||
public class ZipFileProcessorValuesProvider : ValuesProviderBase<IFileValue, IFileValue> | ||
{ | ||
private ZipFileProcessorParams _args; | ||
public ZipFileProcessorValuesProvider(ZipFileProcessorParams args) | ||
=> _args = args; | ||
public override ProcessImpact PerformanceImpact => ProcessImpact.Average; | ||
public override ProcessImpact MemoryFootPrint => ProcessImpact.Average; | ||
public override void PushValues(IFileValue input, Action<IFileValue> push, CancellationToken cancellationToken, IExecutionContext context) | ||
{ | ||
var destinations = (input.Metadata as IFileValueWithDestinationMetadata)?.Destinations; | ||
if (cancellationToken.IsCancellationRequested) return; | ||
using var stream = input.Get(_args.UseStreamCopy); | ||
var ms = new MemoryStream(); | ||
var fileName = $"{input.Name}.zip"; | ||
using (ZipOutputStream zipStream = new ZipOutputStream(ms)) | ||
{ | ||
if (!String.IsNullOrEmpty(_args.Password)) | ||
zipStream.Password = _args.Password; | ||
|
||
var zipEntry = new ZipEntry(fileName) | ||
{ | ||
DateTime = DateTime.Now, | ||
IsUnicodeText = true | ||
}; | ||
|
||
zipStream.PutNextEntry(zipEntry); | ||
stream.CopyTo(zipStream); | ||
zipStream.CloseEntry(); | ||
} | ||
ms.Seek(0, SeekOrigin.Begin); | ||
push(new ZippedFileValue<ZippedFileValueMetadata>(ms, input.Name, new ZippedFileValueMetadata | ||
{ | ||
ParentFileMetadata = input.Metadata, | ||
Destinations = destinations | ||
}, input)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.IO; | ||
using Paillave.Etl.Core; | ||
|
||
namespace Paillave.Etl.Zip | ||
{ | ||
public class ZippedFileValue<TMetadata> : FileValueBase<TMetadata> where TMetadata : IFileValueMetadata | ||
{ | ||
private readonly Stream _stream; | ||
private readonly IFileValue _underlyingFileValue; | ||
public override string Name { get; } | ||
public ZippedFileValue(Stream stream, string name, TMetadata metadata, IFileValue underlyingFileValue) | ||
: base(metadata) | ||
=> (_stream, Name, _underlyingFileValue) | ||
= (stream, name, underlyingFileValue); | ||
public override Stream GetContent() | ||
{ | ||
var ms = new MemoryStream(); | ||
_stream.Seek(0, SeekOrigin.Begin); | ||
_stream.CopyTo(ms); | ||
ms.Seek(0, SeekOrigin.Begin); | ||
return ms; | ||
} | ||
protected override void DeleteFile() | ||
{ | ||
_underlyingFileValue.Delete(); | ||
} | ||
|
||
public override StreamWithResource OpenContent() | ||
{ | ||
_stream.Seek(0, SeekOrigin.Begin); | ||
return new StreamWithResource(_stream); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters