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.

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

  1. C# 使用原生 System.IO.Compression 实现 zip 的压缩与解压

    zip 是一个非常常见的压缩包格式,本文主要用于说明如何使用代码 文件或文件夹压缩为 zip压缩包及其解压操作, 我们采用的是 微软官方的实现,所以也不需要安装第三方的组件包. 使用的时候记得 usi ...

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

    引用: using System.IO.Compression; using (FileStream zipToOpen = new FileStream(@"D:\json.zip&quo ...

  3. System.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

    1.提示错误信息: zipSystem.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' ...

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

    用记事本编辑*.EXE.config,在“<system.net>”节点加入<defaultProxy> <proxy usesystemdefault="Fa ...

  5. Installing MySQL on Microsoft Windows Using a noinstall Zip Archive

    这两天在自己的windows上安装了一下mySql数据库,安装使用的是5.7.18版本的 noinstall Zip Archive安装包mysql-5.7.18-win32.zip.由于5.7版本相 ...

  6. eclipse invalid zip archive lib

    eclipse invalid zip archive lib 修改.class文件的编译内容.删除license或找到相应的jar包,放到lib下面,重新编译.

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

    MySQL Community Server 5.5.56  ZIP Archive  绿色解压版 window安装步骤 首先 准备好启动配置文件my.ini [mysqld] #设置字符集为utf8 ...

  8. System.Span, System.Memory,还有System.IO.Pipelines

    System.Span, System.Memory,还有System.IO.Pipelines 使用高性能Pipelines构建.NET通讯程序 .NET Standard支持一组新的API,Sys ...

  9. Mysql下载(on windows-noinstall zip archive)

    所有内容,都是针对Mysql5.7.18介绍. 1.首先你需要下载一个完整的包,Mysql目前有两个版本可以使用: a. MySql Enterprise Edition:企业版 b. MySql C ...

随机推荐

  1. Hessian Matrix

    函数\(f\)的Hessian矩阵由是由它的二阶偏导数组成的方阵 \[ H = \begin{bmatrix} \dfrac{\partial^2 f}{\partial x_1^2} & \ ...

  2. Linux下磁盘分区挂载

    一般你去买vps都会看到介绍说硬盘多少G  比如 80G 但是你进入系统df -h的时候发现怎么只有10G呢, 其实这10G是用来装系统的和一些常用服务软件的  不是给你放网站数据的 那50G硬盘在哪 ...

  3. html-div中内容自动换行

    <div style='width: 100px;display:block;word-break: break-all;word-wrap: break-word;'> 内容超出div宽 ...

  4. SPAN的高度问题

    FIRST OF ALL,最容易令人忽略而导致头疼的因素.web页面文档类型: 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...

  5. 【Codeforces 707B】Bakery 水题

    对每个storages找一下最短的相邻边 #include <cstdio> #define N 100005 #define inf 0x3f3f3f3f using namespace ...

  6. 【HDU 5750】Dertouzos(数学)

    题目给定n和d,都是10的9次方以内,求1到n里面有几个数最大因数是d?1000000组数据.解:求出d的满足p[i]*d<n的最小质因数是第几个质数.即为答案. #include<cst ...

  7. 多线程中的synchronized

    synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码 ...

  8. hibernate查询返回一个list ,Date类型追加数据

    public Pagination getLookPage(BeanPatrolScheduling beanPatrolScheduling, int pageNo, int pageSize) { ...

  9. 在数据库中如果组合主键(假设为stuID和stuName)存在则更新,不存在则新增

    这是今天在项目中遇到的问题,后来查了一下,有的网友说可以用存储过程,但自己现在还不会用,所以下记载下来,做为学习存贮过程的引子. 现在是在java中实现了这个if的逻辑,

  10. c语言几种异常

    这几天写C程序,问题不断,先记下来吧 double free or corruption 字面意思理解为重复释放空间或崩溃,通常由于你调用了两次free,虽然你可能不是两次给free()传同一个指针, ...