locked
Zip (Compress) a Folder that Contains uploaded documents (VB ASP.NET 3.5) RRS feed

  • Question

  • User-1293774545 posted

     I have a web application using VB ASP.NET 3.5 that along with many things allows users to upload documents into a directory on the web server. I would like to know how I can zip a folder on a webserver and download it to the client machine via a submit button.

    Currently, I have the files displayed as URLs so that the users can right-click and "Save Target As." But I think just having everything zipped in one file would make it alot easier for the users.

    Thanks in advance,

    Phil

    Monday, August 24, 2009 3:03 PM

Answers

  • User653228039 posted

     In that example, zipFromPath is the folder containing all of the files I wish to zip.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 25, 2009 11:49 AM

All replies

  • User653228039 posted

     I use iCSharpZipLib, which looks to have a VB library as well.

    http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

    In C#, the code looks like this:

    protected void ZipFile(String zipToPath, String zipFromPath)
    {
    	if (!Directory.Exists(Server.MapPath(zipFromPath)))
    	{
    		errMsg.Text = "Cannot Find Directory: " +Server.MapPath(zipFromPath).ToString()+"<br/>";
    	}
    	else
    	{
    		try
    		{
    			if(!File.Exists(Server.MapPath(zipToPath)))
    			{
    				FastZip fastZip = new FastZip();
    				fastZip.CreateEmptyDirectories = true;
    				fastZip.RestoreAttributesOnExtract = true;
    				fastZip.RestoreDateTimeOnExtract = true;
    				fastZip.CreateZip(Server.MapPath(zipToPath), Server.MapPath(zipFromPath), true, null);
    			}
    			//You might modify the below to match your method of providing a download link
    			if(File.Exists(Server.MapPath(zipToPath)))
    			{
    				zipFileLink.InnerHtml = "[Click Here to Download File]";
    				zipFileLink.HRef = zipToPath;
    			}
    		}
    		catch(Exception ex)
    		{
    			errMsg.Text += "Exception during processing: "+ex.Message+"<br/>";
    		}
    	}
    }


    Not exactly sure how to do it in VB, but this article discusses one way to do it with the sharpziplib:

    http://www.vbdotnetheaven.com/UploadFile/scottlysle/CompressFoldersVB04262007005440AM/CompressFoldersVB.aspx

    bear in mind, it doesn't use the "FastZip" component like my example above.  I've used this other part of the library before...they both work fine.

    Monday, August 24, 2009 7:43 PM
  • User-1293774545 posted

    Thanks for the reply! How would the code differ if I wanted to zip the entire directory (Folder) instead of individual files?

     

    Tuesday, August 25, 2009 10:43 AM
  • User653228039 posted

     In that example, zipFromPath is the folder containing all of the files I wish to zip.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 25, 2009 11:49 AM