Creating a ZIP Archive in Memory Using System.IO.Compression

Thanks to http://stackoverflow.com/a/12350106/222748 I got:

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);
   }
}

So we need to call dispose on ZipArchive before we can use it, which means passing ‘true‘ as the third parameter to the ZipArchive so we can still access the stream after disposing it.

时间: 2024-10-17 09:08:45

Creating a ZIP Archive in Memory Using System.IO.Compression的相关文章

利用System.IO.Compression操作压缩文件

引用: using System.IO.Compression; using (FileStream zipToOpen = new FileStream(@"D:\json.zip", FileMode.Open)) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) { ZipArchiveEntry readmeEntry = archive.Entries[0]; usi

What to do when the Chinese Characters are messed up when extracting from zip archive?

*/--> What to do when the Chinese Characters are messed up when extracting from zip archive? In some cases, I download zipped files from internet, the files have different encoding as the linux Gnome environment, so the Chinese Characters are all mes

MySQL Community Server 5.5.56 ZIP Archive 绿色解压版 window安装步骤

MySQL Community Server 5.5.56  ZIP Archive  绿色解压版 window安装步骤 首先 准备好启动配置文件my.ini [mysqld] #设置字符集为utf8 loose-default-character-set = utf8 basedir = F:/IDE/mysql5.5 datadir = F:/IDE/mysql5.5/data [client] #设置客户端字符集 loose-default-character-set = utf8 [Wi

Installing MySQL on Microsoft Windows Using a noinstall Zip Archive

这两天在自己的windows7上安装了一下MySQL数据库,安装使用的是5.7.18版本的 noinstall Zip Archive安装包mysql-5.7.18-win32.zip.由于5.7版本相对5.6版本发生了一下变化,很多网上的安装教程都失效了,因此在安装过程中遇到了一系问题,在这里根据自己查看的官方文档和实际的解决过程,整理了一下高版本的MySql的安装步骤,希望对大家有所帮助.详情步骤如下: 1.下载安装包 从https://cdn.mysql.com//Downloads/My

遇到 Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section的解决办法

用记事本编辑*.EXE.config,在“<system.net>”节点加入<defaultProxy> <proxy usesystemdefault="False" /> </defaultProxy> 完整的: <?xml version="1.0"?> <configuration> <system.net> <settings> <httpWebReque

应用商城 下载apk 安装包解析错误 没有权限 Permission denied Android - failed to open zip archive

1.错误提示: 03-31 16:48:43.740: INFO/ActivityManager(59): Start proc com.android.packageinstaller for activity com.android.packageinstaller/.PackageInstallerActivity: pid=620 uid=10026 gids={} 03-31 16:48:44.749: WARN/zipro(620): Unable to open zip '/dat

How can I create a zip archive of a whole directory via terminal without hidden files?

I have a project with lots of hidden folders / files in it. I want to create a zip-archive of it, but in the archive shouldn't be any hidden folders / files. If files in a hidden folder are not hidden, they should also not be included. I know that I

Mysql下载(on windows-noinstall zip archive)

所有内容,都是针对Mysql5.7.18介绍. 1.首先你需要下载一个完整的包,Mysql目前有两个版本可以使用: a. MySql Enterprise Edition:企业版 b. MySql Community Edtion:社区版 我们下载社区版(GPL)介绍,下载地址:https://dev.mysql.com/downloads/mysql/ 打开后,拉到最底部,下载Windows (x86, 64-bit), ZIP Archive,截个图如下: 原因: mysql-5.7.18-

System.IO

             I/O       1.文件操作:File (1)void AppendAllText(string path, string contents) (2)bool Exists(string path) (3)string[] ReadAllLines(string path),读取文本文件到字符串数组中 (4)string ReadAllText(string path),读取文本文件到字符串中 (5)void WriteAllText(string path, st