C# ZIP files to Stream
.NET 8 include new CreateFromDirectory
overloads which enable you to create a ZIP file without writing them to disk. This is particularly useful in situations where you don’t want to store the zipped resources, you use the Zip content only for transfer.
For example: if you were to provide an API allowing multi-file download. That endpoint will most likely compress the selected files into a .zip
and provide that for download. By using the new overloads, you can easily bypass the disk – reducing the load on the disk and offering a faster response time to your users.
public static void CreateFromDirectory(string sourceDirectoryName, Stream destination);
Code language: C# (cs)
In addition to the overload above there are a couple more allowing you to configure some options such as compressionLevel
, includeBaseDirectory
and entryNameEncoding
.
public static void CreateFromDirectory(string sourceDirectoryName, Stream destination, CompressionLevel compressionLevel, bool includeBaseDirectory);
public static void CreateFromDirectory(string sourceDirectoryName, Stream destination, CompressionLevel compressionLevel, bool includeBaseDirectory, Encoding? entryNameEncoding);
Code language: C# (cs)
Extract ZIP file from Stream
Similar to the CreateFromDirectory
overloads, .NET 8 also includes new ExtractToDirectory
overloads which enable you to extract a ZIP file directly from a Stream
, without writing the intermediary .zip
to disk. This is particularly useful in situations where you don’t want to store the Zip file and all you care about are the unzipped resources.
public static void ExtractToDirectory(Stream source, string destinationDirectoryName)
Code language: C# (cs)
In addition to the overload above there are a few more allowing you to configure some options such as overwriteFiles
and entryNameEncoding
.
public static void ExtractToDirectory(Stream source, string destinationDirectoryName) { }
public static void ExtractToDirectory(Stream source, string destinationDirectoryName, bool overwriteFiles) { }
public static void ExtractToDirectory(Stream source, string destinationDirectoryName, Encoding? entryNameEncoding) { }
public static void ExtractToDirectory(Stream source, string destinationDirectoryName, Encoding? entryNameEncoding, bool overwriteFiles) { }
Code language: C# (cs)