@@ -297,18 +297,83 @@ public static async Task<Dictionary<string, string>> UnzipDataAsync(Stream strea
297297 /// <returns>The zipped file as a byte array</returns>
298298 public static byte [ ] ZipBytes ( byte [ ] bytes , string zipEntryName )
299299 {
300- using ( var memoryStream = new MemoryStream ( ) )
300+ using var memoryStream = new MemoryStream ( ) ;
301+ ZipBytesAsync ( memoryStream , bytes , zipEntryName , null ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
302+ return memoryStream . ToArray ( ) ;
303+ }
304+
305+ /// <summary>
306+ /// Performs an in memory zip of the specified bytes in the target stream
307+ /// </summary>
308+ /// <param name="target">The target stream</param>
309+ /// <param name="data">The file contents in bytes to be zipped</param>
310+ /// <param name="zipEntryName">The zip entry name</param>
311+ /// <param name="compressionLevel">The desired compression level</param>
312+ /// <returns>The zipped file as a byte array</returns>
313+ public static async Task ZipBytesAsync ( Stream target , byte [ ] data , string zipEntryName , CompressionLevel ? compressionLevel = null )
314+ {
315+ await ZipBytesAsync ( target , [ new KeyValuePair < byte [ ] , string > ( data , zipEntryName ) ] , compressionLevel ) . ConfigureAwait ( false ) ;
316+ }
317+
318+ /// <summary>
319+ /// Performs an in memory zip of the specified bytes in the target stream
320+ /// </summary>
321+ /// <param name="target">The target stream</param>
322+ /// <param name="data">The file contents in bytes to be zipped</param>
323+ /// <param name="compressionLevel">The desired compression level</param>
324+ /// <returns>The zipped file as a byte array</returns>
325+ public static async Task ZipBytesAsync ( Stream target , IEnumerable < KeyValuePair < byte [ ] , string > > data , CompressionLevel ? compressionLevel = null )
326+ {
327+ using var archive = new ZipArchive ( target , ZipArchiveMode . Create , true ) ;
328+ foreach ( var kvp in data )
301329 {
302- using ( var archive = new ZipArchive ( memoryStream , ZipArchiveMode . Create , true ) )
330+ var entry = archive . CreateEntry ( kvp . Value , compressionLevel ?? CompressionLevel . SmallestSize ) ;
331+ using var entryStream = entry . Open ( ) ;
332+ await entryStream . WriteAsync ( kvp . Key ) . ConfigureAwait ( false ) ;
333+ }
334+ }
335+
336+ /// <summary>
337+ /// Performs an in memory zip of the specified stream in the target stream
338+ /// </summary>
339+ /// <param name="target">The target stream</param>
340+ /// <param name="data">The file contents in bytes to be zipped</param>
341+ /// <param name="mode">The archive mode</param>
342+ /// <param name="compressionLevel">The desired compression level</param>
343+ /// <returns>The zipped file as a byte array</returns>
344+ public static async Task ZipStreamsAsync ( string target , IEnumerable < KeyValuePair < string , Stream > > data , ZipArchiveMode mode = ZipArchiveMode . Create ,
345+ CompressionLevel ? compressionLevel = null )
346+ {
347+ using var fileStream = mode == ZipArchiveMode . Update
348+ ? new FileStream ( target , FileMode . OpenOrCreate , FileAccess . ReadWrite , FileShare . None )
349+ : new FileStream ( target , FileMode . Create , FileAccess . Write , FileShare . None ) ;
350+ await ZipStreamsAsync ( fileStream , data , mode , compressionLevel ) . ConfigureAwait ( false ) ;
351+ }
352+
353+ /// <summary>
354+ /// Performs an in memory zip of the specified stream in the target stream
355+ /// </summary>
356+ /// <param name="target">The target stream</param>
357+ /// <param name="data">The file contents in bytes to be zipped</param>
358+ /// <param name="mode">The archive mode</param>
359+ /// <param name="compressionLevel">The desired compression level</param>
360+ /// <param name="leaveStreamOpen">True to leave the taget stream open</param>
361+ /// <returns>The zipped file as a byte array</returns>
362+ public static async Task ZipStreamsAsync ( Stream target , IEnumerable < KeyValuePair < string , Stream > > data , ZipArchiveMode mode = ZipArchiveMode . Create ,
363+ CompressionLevel ? compressionLevel = null , bool leaveStreamOpen = false )
364+ {
365+ compressionLevel ??= CompressionLevel . SmallestSize ;
366+ using var archive = new ZipArchive ( target , mode , leaveStreamOpen ) ;
367+ foreach ( var kvp in data )
368+ {
369+ if ( archive . Mode == ZipArchiveMode . Update )
303370 {
304- var entry = archive . CreateEntry ( zipEntryName ) ;
305- using ( var entryStream = entry . Open ( ) )
306- {
307- entryStream . Write ( bytes , 0 , bytes . Length ) ;
308- }
371+ var existingEntry = archive . GetEntry ( kvp . Key ) ;
372+ existingEntry ? . Delete ( ) ;
309373 }
310- // 'ToArray' after disposing of 'ZipArchive' since it finishes writing all the data
311- return memoryStream . ToArray ( ) ;
374+ var entry = archive . CreateEntry ( kvp . Key , compressionLevel . Value ) ;
375+ using var entryStream = entry . Open ( ) ;
376+ await kvp . Value . CopyToAsync ( entryStream ) . ConfigureAwait ( false ) ;
312377 }
313378 }
314379
0 commit comments