开发者

Creating virtual directory tree in zip file using dotnetzip

I am trying to create a zip file from code, I'm using 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. I have also tried AddEntry but this requires some content.

My files are stored in a SQL Server using the FileStream option and organised in a hierarchy there.

I wrote this recursive method to do it but the AddDirectory line doesn't work.

    private void GetFiles(ZipFile zipFile, Folder folder, string path)
    {
        zipFile.AddDirectory(folder.Fo开发者_如何学JAVAlderName, 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);
        }
    }


You can use AddDirectoryByName to create a new directory in the zipfile instead of importing a directory


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:

Add a set of items to a zip file, specifying a common directory in the zip archive. This example adds entries to a zip file. 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");
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜