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