#1
  1. I lov C in AIX/Linux, hate C++
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jul 2003
    Location
    Jacksonville, Florida
    Posts
    1,655
    Rep Power
    33

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    297
    Rep Power
    20
    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.
  4. #3
  5. I lov C in AIX/Linux, hate C++
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jul 2003
    Location
    Jacksonville, Florida
    Posts
    1,655
    Rep Power
    33
    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);
  6. #4
  7. Crushing a million faces
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    282
    Rep Power
    94
    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:
    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();
    Last edited by Death Goddess; January 17th, 2008 at 01:30 PM.
    Baby soft, because its made from real babies.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Unhappy


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

IMN logo majestic logo threadwatch logo seochat tools logo