Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a zip file with ionic.zip

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?

like image 626
Richard.Gale Avatar asked Mar 08 '13 10:03

Richard.Gale


People also ask

How do I create a zip file in Terminal?

If you open a terminal console in the parent directory, or used the cd command to navigate there from the command line, you should then be able to run the command on the folder. The syntax is ' zip -r <zip file name> <directory name> '. The '-r' option tells zip to include files/folders in sub-directories.


1 Answers

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, "");
like image 79
Treb Avatar answered Sep 29 '22 20:09

Treb