【问题标题】:ASP.net MVC - IO.Compression library not recreating the folder structure in a ZIP fileASP.net MVC - IO.Compression 库未在 ZIP 文件中重新创建文件夹结构
【发布时间】:2016-02-04 14:34:25
【问题描述】:

我有一个业务场景,我应该允许将加密文件夹下载为 ZIP 文件;这意味着文件夹及其子文件夹中的文件已加密。当我对文件执行服务器端解密时,首先需要在临时文件夹中重新创建相同的文件夹结构,并解密文件本身并将它们保存到临时文件夹的相应文件夹中。

这部分效果很好;临时文件中的文件夹结构按照应有的方式重新创建,但是当我尝试使用 System.IO.Compression 库创建临时文件夹的 ZIP 文件时出现问题。

当您打开 ZIP 文件时,仅将结构的第一个文件夹创建为文件夹,其余文件夹仅创建为无扩展名文件。

解压应用程序看到的文件夹结构

代码 sn-p:

    //Creating temporary folder where the decrypted files will be extracted
    var tempFolderPath = _directory.CreateTemporaryFolder(rootFolderPath);
    var serverTempFolderPath = HttpContext.Current.Server.MapPath(tempFolderPath);

    //Creating the folder structure
    RecursiveFolderCreate(folders, tempFolderPath);

    //Decrypting the files and recreating them in the corresponding folders
    foreach (var file in files)
    {
        PrepareFile(file, folderPath, tempFolderPath);
    }

    //Creating the zip file
    string zipFileName = string.Concat(folder.Name, ".zip");
    string zipFilePath = System.IO.Path.Combine(rootFolderPath, zipFileName);
    string serverZipFilePath = HttpContext.Current.Server.MapPath(zipFilePath);

    ZipFile.CreateFromDirectory(serverTempFolderPath, serverZipFilePath, CompressionLevel.Optimal, false);
    byte[] zipFile = System.IO.File.ReadAllBytes(serverZipFilePath);

【问题讨论】:

    标签: c# asp.net asp.net-mvc compression zipfile


    【解决方案1】:

    对不起,这不是评论,但我还没有足够的声誉来发表评论。

    是否有可能引发了异常但未处理?

    根据文档 (https://msdn.microsoft.com/en-us/library/hh485721(v=vs.110).aspx/),您所做的应该可以工作。但它有如下所列的额外警告:

    如果无法将目录中的文件添加到存档中,则存档不完整且无效,并且该方法会引发 IOException 异常。

    【讨论】:

    • 感谢您的回答,但不幸的是,我在 catch 块中没有得到任何东西。
    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 2011-11-09
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2011-09-21
    相关资源
    最近更新 更多