问题描述
我需要将3个文件合并为1个zip文件,并可供用户下载.除了一件事情,我能够满足我的要求:它将文件压缩到子文件夹中.
I need to combine 3 files into 1 zip file and make it available to download for the user. I am able to achieve my requirement except one thing: it zips the files into the subfolders.
例如,我的文件位于以下位置:
For example, my files are located like the following:
C:\TTCG\WebSites\Health\ABC.CSV
C:\TTCG\WebSites\Health\XYZ.CSV
C:\TTCG\WebSites\Health\123.CSV
但是在压缩文件中,它使用"TTCG \ WebSites \ Health \"作为路径来压缩文件夹中的文件.请参阅附件.
But in the zip file, it zip the files in the folder by using "TTCG\WebSites\Health\" as the path. Please see the attach file.
我不希望路径中的文件夹.我只希望zip文件中的3个文件没有文件夹.我该如何实现?
I don't want the folders in the path. I just want 3 files in the zip file without folders. How can I achieve that?
我生成zip文件的代码如下:
My codes to generate the zip file is as below:
ZipFile z = ZipFile.Create(Server.MapPath("~" + @"\Accident.zip"));
//initialize the file so that it can accept updates
z.BeginUpdate();
//add the file to the zip file
z.Add(Server.MapPath("~" + @"\ABC.csv"));
z.Add(Server.MapPath("~" + @"\XYZ.csv"));
z.Add(Server.MapPath("~" + @"\123.csv"));
//commit the update once we are done
z.CommitUpdate();
//close the file
z.Close();
推荐答案
基于常见问题解答,您必须手动删除文件夹路径:
Based on the FAQ, you have to strip the folder path out manually:
删除用于创建ZipEntry的文件名的路径部分 在将其添加到ZipOutputStream之前
Remove the path portion of the filename used to create a ZipEntry before it is added to a ZipOutputStream
ZipEntry entry = new ZipEntry(Path.GetFileName(fullPath));
可以在此处找到该常见问题解答.
The FAQ can be found here.
这似乎是图书馆的限制.希望这会有所帮助!
It seems to be a limitation of the library. Hope this helps!
这篇关于使用SharpZipLib将文件添加到没有路径的ZIP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!