.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2008, 05:31 PM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,654 fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 35 m 40 sec
Reputation Power: 32
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.

Reply With Quote
  #2  
Old January 16th, 2008, 07:21 PM
Direhit Direhit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 297 Direhit User rank is Sergeant (500 - 2000 Reputation Level)Direhit User rank is Sergeant (500 - 2000 Reputation Level)Direhit User rank is Sergeant (500 - 2000 Reputation Level)Direhit User rank is Sergeant (500 - 2000 Reputation Level)Direhit User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 2 h 32 m 30 sec
Reputation Power: 19
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.

Reply With Quote
  #3  
Old January 17th, 2008, 08:12 AM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,654 fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 35 m 40 sec
Reputation Power: 32
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);

Reply With Quote
  #4  
Old January 17th, 2008, 01:28 PM
Death Goddess's Avatar
Death Goddess Death Goddess is offline
Crushing a million faces
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 282 Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 5 Days 11 h 25 m 14 sec
Reputation Power: 93
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.

Reply With Quote
  #5  
Old February 1st, 2013, 01:58 AM
codebreaker7 codebreaker7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 codebreaker7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 49 sec
Reputation Power: 0
Unhappy

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..

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > HttpWebResponse - Save zip file locally but file is corrupted when unzipped..

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap