How to download files from given Url for zipping

Dec 4, 2009 at 9:35 AM

Hi All,

   I am trying to use the zip.AddFile (HttpContext.Current.Server.MapPath(http://sharepointsite/abcd.doc).,"") method to add the files to the zip folder for zipping but it gives the error

"http://sharepointsite/abcd.doc is not a valid virtual path." . Is there any other way to add a file to the zip folder by passing the file's Url in the zip.AddFile() method ? It is very urgent.

 

Thanks

Elizabeth

 

Dec 4, 2009 at 10:13 AM

Hi,

I forgot to mention that the files on which the zipping operation is to be performed will reside in a sharepoint document library that can be accessed from the corresponding sharepoint site.

Coordinator
Dec 5, 2009 at 1:13 PM

Servver.MapPath requires a filesystem path, not a URL. 

(This has nothing to do with DotNetZip).

> Is there any other way to add a file to the zip folder by passing the file's Url in the zip.AddFile() method ?

Yes, you can create a zip entry from a URL, by first opening a stream on the URL.  There's a handy class in the .NET BCL for this:  System.Net.WebClient.  Call the OpenRead() method on that class, which gives you a stream.  Then pass that stream to the zipFile.AddEntry() method that accepts a stream.

it looks like this:
// Create a new WebClient instance.
System.Net.WebClient wc = new System.Net.WebClient();
// get the stream for a particular URL
Stream s = wc.OpenRead("http://server/foo"); 
// add an entry into the zipfile using that stream as input
zip.AddEntry("MyEntryName.txt", s);
// close the stream
s.Close(); 

<!---->
Dec 7, 2009 at 5:01 AM
Edited Dec 7, 2009 at 5:15 AM

Thanks a lot you that would simplify my code drastically

 

Dec 7, 2009 at 12:02 PM

Hi,

     I followed the above method using OpenRead() and  tried using DownloadData() method for stream and byte[] respectively.

It works fine as long as the document getting zipped is a .txt document but for the other types of documents, its zips the document

but when I try to open the document it gives the message that the document format is not supported and opens the document with data in a wrong format.

Any ideas ?

 

Ps: the OpenRead() and DownloadData() are both passed to a zip.AddEntry() method .

 

Thanks

Elizabeth

Coordinator
Dec 7, 2009 at 2:13 PM

Hi Elizabeth,

If I were doing this I would ...:

  • download the URL directly, save it, and try to open it.  Does it work?
  • do the zip.AddEntry() thing, and then save.  Then, open the zip.  Verify that the name on the zip entry - in particular the filename extension - is the same as the name of the file you downloaded directly.  if the name doesn't agree, then you have a naming problem. 
  • open the zip again, programmatically, unzip the entry you added, and compare the bytes received there, with the bytes directly downloaded.  Same size?  Same content?  If not then something is changing them.

I don't know what you mean by "it gives the message that the document format is not supported".  What is "it" that is giving you this message?  How are you opening the zip file?  Are you sure the name attached to the zip entry is appropriate for the content?  In Windows the extension of the file dictates how it will be opened.  A .pdf file is treated as an Adobe Acrobat file., for example.  If you put regular text content into a file, name it with a .pdf extension, and then try to open it by double clicking in explorer, explorer will complain about a corrupt file.  My point is that if the extension on the filename is not consistent with the content within the file, you can get a message similar to what you described. 

-Cheeso

 

Dec 8, 2009 at 10:08 AM

Currently what I am doing is

  • I am downloading the file from the url using the web client's DownloadFile() method into a temporary folder.
  • Using the AddFile() method in DotNetZip I add the file passing its temporary folder's filepath as the parameter to the AddFile() method.
  • Using the Save() method in DotNetZip I save it.

In this way all the files are zipped into a zip file.

When i check the contents of the temporary folder all the files and its contents are in the correct formats irrespective of file type.

However when I open the zipped file and double click on a file other than a .txt file an incorrect format message is given and on opening the file its contents are not in the right format.

To cross check the contents of the Zipped file I programmatically unzipped the zipped file .All the unzipped files were now in the correct format with the right contents.

 

Any ideas why this change occurs?

Ps: I have saved the files with the right file extentions and the contents of the files are in sync with their respective file types.

Thanks

Elizabeth

Coordinator
Dec 8, 2009 at 12:30 PM

However when I open the zipped file and double click on a file other than a .txt file an incorrect format message is given and on opening the file its contents are not in the right format.

To cross check the contents of the Zipped file I programmatically unzipped the zipped file .All the unzipped files were now in the correct format with the right contents.

If you can unzip the files programmatically, and then open them, that would suggest that the content is correct within the zip file.  

you didn't say what you use to "open the zipped file and double click". Explorer?

You might try to open a zipped file from another source, and double-click, and see what happens.

Dec 8, 2009 at 12:40 PM

I am saving the zipped file on my desktop so I am double clicking on this zipped file when an incorrect format message is given and then

when i proceed to open the file its contents are not in the right format.

I am sorry but I did not understand what you meant in the last statement.

 

Coordinator
Dec 8, 2009 at 2:12 PM

Are you saying that the error message occurs when you double-click the zip file itself?

Dec 9, 2009 at 7:48 AM

No, it happens after I have opened the zip file by double-clicking the zip file.Then when I click on an individual file in the zip file of any file type other than .txt type

an incorrect format message is given and then when i proceed to open the file its contents are not in the right format.

 

Dec 9, 2009 at 12:25 PM

Hi,

 

The problem is resolved. It seemed to have been a machine specific problem so on running the code on other systems the code works fine .

 

Thanks for all the help

Elizabeth