The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> .Net Development
|
HttpWebResponse - Save zip file locally but file is corrupted when unzipped..
Discuss HttpWebResponse - Save zip file locally but file is corrupted when unzipped.. in the .Net Development forum on Dev Shed. HttpWebResponse - Save zip file locally but file is corrupted when unzipped.. .NET development forum discussing all .NET derivatives including C#, VB.NET, ASP.NET, ADO.NET and more. Learn the ins and outs of using the .NET framework.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 16th, 2008, 05:31 PM
|
 |
I lov C in AIX/Linux, hate C++
|
|
Join Date: Jul 2003
Location: Jacksonville, Florida
|
|
|
HttpWebResponse - Save zip file locally but file is corrupted when unzipped..
I wrote a script that where the HttpWebRequest object had fetch a zip file from the webserver and save it locally behind the scene. This script worked beautifully. However, I have one problem. When I tried to unzip the file, it is corrupted. The error said "This archive is not a valid zip file". So, I don't see what's wrong with the script below that would cause the zip file to get corrupted. Did I leave something out? Let me know...
Code:
string sFullFilePath = "C:\test.zip";
oHttpWebResponse = (System.Net.HttpWebResponse)(oHttpWebRequest.GetResponse());
iContentLength = Convert.ToInt32(oHttpWebResponse.ContentLength);
bFile = new byte[iContentLength];
oHttpWebResponse.GetResponseStream().Read(bFile, 0, iContentLength);
oStream = System.IO.File.Create(sFullFilePath);
oStream.Write(bFile, 0, bFile.Length);
thanks...
Last edited by fletchsod : January 16th, 2008 at 05:35 PM.
|

January 16th, 2008, 07:21 PM
|
|
|
|
Set GetResponseStream() to a File.IO.Stream and then try using System.IO.FileStream with options FileMode.OpenOrCreate and
FileAccess.Write to write the buffer out to a file rather then creating a file using System.IO.File.Create.
|

January 17th, 2008, 08:12 AM
|
 |
I lov C in AIX/Linux, hate C++
|
|
Join Date: Jul 2003
Location: Jacksonville, Florida
|
|
When I went home last night, later on that night, it just dawn on me that I write a custom debugger script so I can look into the byte array to find out what happened to the. From there, I found the first 1/3 had been received and the rest are all zero. So I was thinking that either the GetResponseStream() got interrupted or wasn't done in timely manner or something. That's when I learned that I need to do a loop so the script can keep going until it is done with some kind of offset to the GetResponseStream() object. It works now. Most of the example I see on the website showed that most people don't know much about these objects and doesn't know how to do it properly...
Code:
string sFullFilePath = "C:\test.zip";
oHttpWebResponse = (System.Net.HttpWebResponse)(oHttpWebRequest.GetResponse());
iContentLength = Convert.ToInt32(oHttpWebResponse.ContentLength);
bFileWriteData = new byte[iContentLength];
while (iContentLengthCounter < iContentLength)
{
iContentLengthCounter += oHttpWebResponse.GetResponseStream().Read(bFileWriteData, iContentLengthCounter, (iContentLength - iContentLengthCounter));
}
oFileStream = new System.IO.FileStream(sFullFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
oFileStream.Write(bFileWriteData, 0, bFileWriteData.Length);
|

January 17th, 2008, 01:28 PM
|
 |
Crushing a million faces
|
|
|
|
Quote: | Originally Posted by fletchsod I wrote a script that where the HttpWebRequest object had fetch a zip file from the webserver and save it locally behind the scene. This script worked beautifully. However, I have one problem. When I tried to unzip the file, it is corrupted. The error said "This archive is not a valid zip file". So, I don't see what's wrong with the script below that would cause the zip file to get corrupted. Did I leave something out? Let me know...
Code:
string sFullFilePath = "C:\test.zip";
oHttpWebResponse = (System.Net.HttpWebResponse)(oHttpWebRequest.GetResponse());
iContentLength = Convert.ToInt32(oHttpWebResponse.ContentLength);
bFile = new byte[iContentLength];
oHttpWebResponse.GetResponseStream().Read(bFile, 0, iContentLength);
oStream = System.IO.File.Create(sFullFilePath);
oStream.Write(bFile, 0, bFile.Length);
thanks... |
HttpWebResponse is tricky. Here's a blurb from the MSDN documentation:
Quote: | You should never directly create an instance of the HttpWebResponse class. Instead, use the instance returned by a call to HttpWebRequest.GetResponse. You must call either the Stream.Close or the HttpWebResponse.Close method to close the response and release the connection for reuse. It is not necessary to call both Stream.Close and HttpWebResponse.Close, but doing so does not cause an error. |
HttpStreams are also a little different from a normal file stream, because you don't have random access to them. You normally have to read the contents of a HttpStream into a MemoryStream to gain random access, which seems a little excessive.
You might have better luck using the System.Net.WebClient object instead. WebClient has a method called DownloadData which returns a stream as a byte array.
You should also use the BinaryWriter object also so that you aren't inadvertantly ASCII encoding your byte array.
Something like this should work:
Code:
WebClient myWebClient = new WebClient();
byte[] data = myWebClient.DownloadData (someUrl);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(sw.BaseStream);
bw.Write(data);
bw.Close();
__________________
Baby soft, because its made from real babies.
Last edited by Death Goddess : January 17th, 2008 at 01:30 PM.
|

February 1st, 2013, 01:58 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 1
Time spent in forums: 25 m 49 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by fletchsod I wrote a script that where the HttpWebRequest object had fetch a zip file from the webserver and save it locally behind the scene. This script worked beautifully. However, I have one problem. When I tried to unzip the file, it is corrupted. The error said "This archive is not a valid zip file". So, I don't see what's wrong with the script below that would cause the zip file to get corrupted. Did I leave something out? Let me know...
Code:
string sFullFilePath = "C:\test.zip";
oHttpWebResponse = (System.Net.HttpWebResponse)(oHttpWebRequest.GetResponse());
iContentLength = Convert.ToInt32(oHttpWebResponse.ContentLength);
bFile = new byte[iContentLength];
oHttpWebResponse.GetResponseStream().Read(bFile, 0, iContentLength);
oStream = System.IO.File.Create(sFullFilePath);
oStream.Write(bFile, 0, bFile.Length);
thanks... | Hi, Can you post the complete code?
I am facing the same problem.
I am posting a *.zip file from my client side app (on android) and receiving it at the server side (using C#.net on IIS 7).
But am unable to open the received *.zip file as winrar/7zip says that it is an corrupted archive
I guess the headers are being saved too..but AFAIK the HTTP POST method in my client side app isn't posting any headers just the zip file (I am using "application/zip) as the content-type on both client/server side.
Any input is appreciable..
Thanks..
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|