如何将文件添加到现有的zip压缩包 [英] How do I add files to an existing zip archive

查看:1166
本文介绍了如何将文件添加到现有的zip压缩包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能某些文件(几乎总是一个.csv文件)添加到现有的zip文件?

How can I add some file (almost always a single .csv file) to an existing zip file?

推荐答案

由于你是在.NET 4.5,可以使用ZipArchive(System.IO.Compression)类来实现这一目标。下面是MSDN文档:( MSDN )。

Since you are in .NET 4.5, you can use the ZipArchive (System.IO.Compression) class to achieve this. Here is the MSDN documentation: (MSDN).

下面是他们的榜样,它只是写的文字,但你可以在.csv文件中读取并写入到新的文件。只是在复制文件,你可以使用 CreateFileFromEntry ,这是一个扩展方法 ZipArchive

Here is their example, it just writes text, but you could read in a .csv file and write it out to your new file. To just copy the file in, you would use CreateFileFromEntry, which is an extension method for ZipArchive.

using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open))
{
   using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
   {
       ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
       using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
       {
           writer.WriteLine("Information about this package.");
           writer.WriteLine("========================");
       }
   }
}

这篇关于如何将文件添加到现有的zip压缩包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆