在C#中将文件添加到zip文件内的文件夹中 [英] Adding files into a folder inside a zip file in c#

查看:709
本文介绍了在C#中将文件添加到zip文件内的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的zip文件代码如下

My code to zip files is as follows

ZipArchive zip = ZipFile.Open(destToZip, ZipArchiveMode.Create);
zip.CreateEntry("pubEd/");

string[] fileEntries = Directory.GetFiles(dirToZip);
foreach (string fileName in fileEntries)
    zip.CreateEntryFromFile(fileName,Path.GetFileName(fileName), CompressionLevel.Optimal);

zip.Dispose();

在创建了zip文件后,在代码的第二行中,我在该zip文件中创建了一个名为pubEd的文件夹.

In the second line of the code once after creating a zip file I create a folder with a name pubEd inside the zip file.

在下一行中,我将文件添加到zip文件夹中.

In the next line I am adding files to the zip folder.

正在发生的事情是直接将文件添加到zip中.

What is happening is files get added to the zip directly.

我想将这些文件添加到我在zip文件中创建的目录中.

I want to add these files inside the directory which I created inside the zip.

我该怎么做?

推荐答案

通过外观,您可以将Path.GetFileName(fileName)更改为"pubEd/" + Path.GetFileName(fileName).并摆脱第二行.那只是基于我对文档的阅读.我实际上还没有尝试过.

By the looks of it you would changePath.GetFileName(fileName) to "pubEd/" + Path.GetFileName(fileName). And get rid of the second line. Thats just based on my reading of the documentation. I have not actually tried it.

这篇关于在C#中将文件添加到zip文件内的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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