Partial filenames with wildcards?

Feb 3, 2010 at 10:25 PM
Edited Feb 3, 2010 at 10:45 PM

Using Ionic.Zip v1.9.1.2, I am trying to add a file to the zip that contains the word "guidelines" (ie *guidelines.pdf)  It skips right over the file and doesn't add anything.  Here is the code:

using (ZipFile zip = new ZipFile())
            {
                // Iterate through the Products.Rows property
                foreach (GridViewRow row in dgFiles.Rows)
                {
                    // Access the CheckBox
                    CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                    if (cb != null && cb.Checked)
                    {
                        // filesToInclude is a IEnumerable (String[] or List etc)
                        fileName = (row.FindControl("lblFileName") as Label).Text;
                        zip.AddFile(Path.Combine(local_path, fileName)), "");

                    }
                }
                zip.AddSelectedFiles(Path.Combine(local_path, "*guidelines.pdf")), "");
                zip.Save(ZipFileName);
            }

Any ideas?

Feb 3, 2010 at 10:27 PM
Edited Feb 3, 2010 at 11:12 PM

Also tried using just "*.pdf" in place and it just skips right over it...if I change it to:

zip.AddFile(Path.Combine(local_path, "*guidelines.pdf")), ""); if says Invalid characters in Path

Coordinator
Feb 3, 2010 at 11:12 PM

Steven

AddFile() does not accept wildcards.

AddSelectedFiles() accepts a string that specifies criteria, and in that you can use wildcards.   If you want to add all *.pdf files in a particular directory, then you should use the AddSelectedFiles() overload that includes a directoryOnDisk parameter.   You cannot specify the criteria as a full path with a * in it. 

YES:   zip.AddSelectedFiles("*.pdf", "c:\\data files");

NO:  zip.AddSelectedFiles("c:\\data files\\*.pdf", "");

Check the documentation for a full description of these methods, including example code.

Feb 4, 2010 at 8:45 PM
Edited Feb 4, 2010 at 8:48 PM

Ok, I got this to work: 

     zip.AddSelectedFiles("*guidelines.pdf", imageLocation + local_path);

But it puts the entire folder path Inetpub/wwwroot/logorepos/file_repository/facility

I tried adding the quotes to put it as no directory in the zip files, but it does it anyway...I have to have some sort of virtual path in there because it will change when a user selects a different facility and the guidelines document changes with each facility...any ideas why it is putting the complete folder path?  Thank you in advance!!

Coordinator
Feb 4, 2010 at 11:27 PM

> any ideas why it is putting the complete folder path?

Yes - that's the intended behavior.

If you want to specify a root folder path for the files, then there's another overload of AddSelectedFiles(), that lets you specify the directory to use within the archive:

ZipFile.AddSelectedFiles(selectionCriteria, directoryOnDisk, directoryInArchive);

All arguments are strings.   If you specify "" as the directoryInArchive, then there is no path used on the entries added to the zip. 

There's yet another overload that allows you to specify whether you want to recurse directories, for the selection. 

I just checked the documentation on these methods, and it's really not explicit enough about this behavior, so I'm rewriting the descriptions so that it will be clearer.

Thanks for the question.

 

Feb 5, 2010 at 12:20 AM

> ZipFile.AddSelectedFiles(selectionCriteria, directoryOnDisk, directoryInArchive);

Here is my code:

string fileName = String.Empty;
            string ZipFileName = Server.MapPath(@"TempFiles/" + Guid.NewGuid().ToString() + ".zip");
            string local_path = tvDirTree.SelectedNode.Value.ToString();
            local_path = "." + local_path.Substring(local_path.IndexOf("file_repository") - 1, (local_path.Length - local_path.IndexOf("file_repository")) + 1).Replace("\\", "/");
            string imageLocation = "D:\\Inetpub\\wwwroot\\logorepos" + local_path;
            string namePath = "";
            string guideName = "*guidelines.pdf";
           

using (ZipFile zip = new ZipFile())
            {
                // Iterate through the Products.Rows property
                foreach (GridViewRow row in dgFiles.Rows)
                {
                    // Access the CheckBox
                    CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                    if (cb != null && cb.Checked)
                    {
                        // filesToInclude is a IEnumerable (String[] or List etc)
                        fileName = (row.FindControl("lblFileName") as Label).Text;
                        zip.AddFile(Server.MapPath(Path.Combine(local_path, fileName)), "");

                    }
                }
                zip.AddSelectedFiles(guideName, imageLocation, "");
                zip.Save(ZipFileName);
            }

It still adds the path to the guidelines.pdf...not sure what I am doing wrong?

Coordinator
Feb 5, 2010 at 12:38 PM

I'll have to look into this.  will get back to you.

Coordinator
Feb 5, 2010 at 12:52 PM

I just tried a simplified version of your code and did not get the results you reported.

In my zip file, the zip entry for the guidelines.pdf file does not get any path. 

The code looks like this:

  string ZipFileName = "swood.zip";
  string imageLocation = "c:\\dev\\dotnet\\zip\\test\\swood\\repository";
  string guideName = "*guidelines.pdf";
  using (ZipFile zip = new ZipFile())
  {
      zip.AddSelectedFiles(guideName, imageLocation, "");
      zip.Save(ZipFileName);
  }

Can you run something like this and tell me the results? 

Feb 5, 2010 at 4:55 PM

Yes, it worked if I changed the imageLocation to an absolute path of:

string imageLocation = "D:\\Inetpub\\wwwroot\\logorepos\\file_repository\\Willows\\";

But this won't work for me since I need the imageLocation to be able to change in relation to what directory the end user is wanting to download from.   I need it to be able to do this:

string imageLocation = "D:\\Inetpub\\wwwroot\\logorepos\\file_repository\\" + local_path;

Is this possible without having the entire file path added to the .zip file?

Coordinator
Feb 5, 2010 at 5:59 PM

I don't understand the distinction you're making.

Whether the imageLocation is initialized with a constant string, or built in some other way, it is still just a string.  It specifies the location of the pdf file you want to zip.  How it is initialized will have no effect on the behavior of the DotNetZip code.

 

Feb 5, 2010 at 9:18 PM

Well, not sure what I did, but it works now...thanks Cheeso!!