Pregunta

I have the following code set up to create a zip file of a set of doucments:

    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:

e.g

I am creating a zip of a selection of documents located at

C:\SoftwareDevelopment\Branches\ScannedDocuments\

When the created zip file is opened, there is a folder structure within the zip as follows:

Folder 1 ("SoftwareDevelopment")

Inside Folder 1 is folder 2 ("Branches")

Inside Folder 2 is folder 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?

¿Fue útil?

Solución

The documentation states that the third parameter

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. Passing null (Nothing in VB) will use the path on each of the fileNames, if any. 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

loanZip.AddFiles(documentPaths, false, zipDestinationPath);

to

loanZip.AddFiles(documentPaths, false, "");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top