简体   繁体   English

使用dotnetzip在zip文件中创建虚拟目录树

[英]Creating virtual directory tree in zip file using dotnetzip

I am trying to create a zip file from code, I'm using dotnetzip 我正在尝试从代码创建一个zip文件,我正在使用dotnetzip

I want to create a directory tree in the folder that doesn't exist on disk. 我想在磁盘上不存在的文件夹中创建目录树。 How do I do this? 我该怎么做呢?

I have tried using AddDirectory but this seems to want to find the directory on disk. 我曾尝试使用AddDirectory但这似乎想在磁盘上找到目录。 I have also tried AddEntry but this requires some content. 我也尝试过AddEntry,但这需要一些内容。

My files are stored in a SQL Server using the FileStream option and organised in a hierarchy there. 我的文件使用FileStream选项存储在SQL Server中,并在那里按层次结构进行组织。

I wrote this recursive method to do it but the AddDirectory line doesn't work. 我编写了此递归方法来执行此操作,但是AddDirectory行不起作用。

    private void GetFiles(ZipFile zipFile, Folder folder, string path)
    {
        zipFile.AddDirectory(folder.FolderName, path);

        foreach (var file in folder.Files)
            zipFile.AddEntry(file.FileName, file.FileData);

        foreach(var subfolder in folder.SubFolders)
        {
            GetFiles(zipFile, subfolder, path + "\\" + subfolder.FolderName);
        }
    }

您可以使用AddDirectoryByNamezip文件中创建新目录,而不是导入目录

It seems from their examples page, if you specify the full filel path, it will add an entry in the ZIP corresponding to that path.. therefore you can try just adding the files with full path and skip the AddDirectory step.. At least this is what I could gather from this code sample in their documentation: 从他们的示例页面看来,如果指定完整的filel路径,它将在ZIP中添加与该路径相对应的条目。因此,您可以尝试仅添加具有完整路径的文件,并跳过AddDirectory步骤。我可以从他们的文档中的此代码示例中收集到什么:

Add a set of items to a zip file, specifying a common directory in the zip archive. 将一组项目添加到zip文件中,并在zip归档文件中指定一个公共目录。 This example adds entries to a zip file. 本示例将条目添加到zip文件中。 Each entry is added using the specified pathname. 每个条目都使用指定的路径名​​添加。

 String[] filenames = { "ReadMe.txt", "c:\\data\\collection.csv", "c:\\reports\\AnnualSummary.pdf"};
  using (ZipFile zip = new ZipFile())
  {
    zip.AddFiles(filenames, "files");
    zip.Save("Archive.zip");
  }

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

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