


How to Properly Create a ZIP Archive in Memory Using MemoryStream and System.IO.Compression?
Jan 07, 2025 am 12:30 AMUsing MemoryStream to Create a ZIP Archive in Memory with System.IO.Compression
While trying to create a ZIP archive using a MemoryStream, you may encounter an issue where the archive is generated but the desired file is missing. This is because ZipArchive requires final bytes to be written for completion. To resolve this:
using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var demoFile = archive.CreateEntry("foo.txt"); using (var entryStream = demoFile.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write("Bar!"); } } using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create)) { memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(fileStream); } }
Passing "true" as the third parameter to ZipArchive allows you to continue using the stream after calling Dispose, ensuring that all necessary information is written to the archive.
The above is the detailed content of How to Properly Create a ZIP Archive in Memory Using MemoryStream and System.IO.Compression?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C language function format letter case conversion steps

What are the types of values returned by c language functions? What determines the return value?

What are the definitions and calling rules of c language functions and what are the

How does the C Standard Template Library (STL) work?

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?
