简体   繁体   English

C#的zip文件到用户包含所有目录?

[英]c# zip file to user containing all directories?

I have a function which zips files and downloads them to the users machine. 我有一个压缩文件并将其下载到用户计算机的功能。

However, when it zips the files they are within a few folders. 但是,当压缩文件时,它们位于几个文件夹中。

For example if i want to zip a file called test.doc and it is in c:/document/the folder/test.doc 例如,如果我想压缩一个名为test.doc的文件,并且它位于c:/ document / the folder / test.doc中

In the zipped folder there will be "document" folder and "the folder" too. 在压缩文件夹中,还将有“文档”文件夹和“文件夹”。 I just want the document in there 我只想把文件放在那里

here is my code... 这是我的代码...

 public FileStreamResult DownloadDocs()
    {
        MemoryStream workStream = new MemoryStream();
        ZipFile zip = new ZipFile();
        string[] fileEntries = Directory.GetFiles(Server.MapPath(SettingManager.OnlineForms));
        foreach (string fileName in fileEntries)
        {
            FileInfo fi = new FileInfo(fileName);
            string name = Server.MapPath(SettingManager.OnlineForms + fi.Name);
            zip.AddFile(name);
        }

        zip.Save(workStream);
        workStream.Position = 0;

        FileStreamResult fileResult = new FileStreamResult(workStream, System.Net.Mime.MediaTypeNames.Application.Zip);
        fileResult.FileDownloadName = "OnlineForms.zip";

        return fileResult;
    }

Change the line 换线

Zip.AddFile(name);

to

Zip.AddFile(name, string.Empty);

Also you could avoid the call to get the fileinfo. 另外,您可以避免调用来获取fileinfo。 The array of file names should suffice. 文件名的数组应足够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM