简体   繁体   English

使用ionic.zip创建一个zip文件

[英]Creating a zip file with ionic.zip

I have the following code set up to create a zip file of a set of doucments: 我设置了以下代码来创建一组doucments的zip文件:

    public bool CreateDocumentationZipFile(int documentIdentifier, string zipDestinationPath, IList<string> documentPaths)        
    {
        bool zipped = false;

        if (documentPaths.Count > 0)
        {
            using (ZipFile loanZip = new ZipFile())
            {
                loanZip.AddFiles(documentPaths, false, zipDestinationPath);
                loanZip.Save(string.Format("{0}{1}.zip",zipDestinationPath, documentIdentifier.ToString()));
                zipped = true;
            }
        }

        return zipped;
    }

The issue I have is that when the zip file is created, the folder structure is maintaned within the zip file: 我的问题是,当创建zip文件时,文件夹结构将保存在zip文件中:

eg 例如

I am creating a zip of a selection of documents located at 我正在创建一个位于的文档选择的zip文件

C:\\SoftwareDevelopment\\Branches\\ScannedDocuments\\ C:\\ SoftwareDevelopment \\分行\\ ScannedDocuments \\

When the created zip file is opened, there is a folder structure within the zip as follows: 打开创建的zip文件时,zip中有一个文件夹结构,如下所示:

Folder 1 ("SoftwareDevelopment") 文件夹1(“SoftwareDevelopment”)

Inside Folder 1 is folder 2 ("Branches") 内部文件夹1是文件夹2(“分支”)

Inside Folder 2 is folder 3 ("ScannedDocuments") 文件夹2内是文件夹3(“ScannedDocuments”)

the scanned documents folder then contains the actual scan files. 扫描的文档文件夹然后包含实际的扫描文件。

Can anyone tell me how I can just have the scan files in the zip without the folders path being maintained? 任何人都可以告诉我如何在没有维护文件夹路径的情况下将zip文件放在zip中?

The documentation states that the third parameter 文档说明了第三个参数

directoryPathInArchive (String) directoryPathInArchive(String)
Specifies a directory path to use to override any path in the file name. 指定用于覆盖文件名中任何路径的目录路径。 This path may, or may not, correspond to a real directory in the current filesystem. 该路径可以或可以不对应于当前文件系统中的真实目录。 If the files within the zip are later extracted, this is the path used for the extracted file. 如果稍后提取zip中的文件,则这是用于提取文件的路径。 Passing null (Nothing in VB) will use the path on each of the fileNames, if any. 传递null(在VB中为Nothing)将使用每个fileNames上的路径(如果有)。 Passing the empty string ("") will insert the item at the root path within the archive. 传递空字符串(“”)将在归档中的根路径中插入项目。

So if you always want to have the files added to the root of your zip archive, change 因此,如果您始终希望将文件添加到zip存档的根目录中,请进行更改

loanZip.AddFiles(documentPaths, false, zipDestinationPath);

to

loanZip.AddFiles(documentPaths, false, "");

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

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