Skip to content

Downloading zip files.

rick

Asked Jun 1, 2006, 5:50 AM EDT3 answers
Share
LinkedInX
Hey guys,

Ive been having a problem downloading files through a proxy server.  it worked fine while i was downloading small test files (zip files to be exact < 5kb) but when i use actual files, they are always corrupt on the client side, but when you open the zip file there are never more than 2 files present and they are corrupt as well.  any help would be greatly appreciated.

Thanks,


here is a before and after screen shot of the zip files, if that helps, and the following is a code snippit.


public void BeginDownload(){
Java
  WebProxy proxy = new WebProxy(ServPath, true);
  proxy.Credentials = new NetworkCredential(UserName, Pass);
  WebRequest request = WebRequest.Create(ServPath + FileName);
  request.Proxy = proxy;
  // Send the 'HttpWebRequest' and wait for response.
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  System.IO.Stream stream = response.GetResponseStream();
  FileStream writeStream = new FileStream(DownloadPath + FileName, FileMode.Create, FileAccess.Write);
  ReadWriteStream(stream, writeStream, response.ContentLength);
  response.Close();
}


private void ReadWriteStream(Stream readStream, Stream writeStream, long lLength){
  int Length = Convert.ToInt32(lLength);
  Byte[] buffer = new Byte[Length + (100 - Length%100) + 5];
  int numBytesToRead = Length;
  int numBytesRead = 0;
  while (numBytesToRead > 0){
    int n;
    n = readStream.Read(buffer, numBytesRead, numBytesToRead);
    WriteStream.Write(buffer, numBytesRead, numBytesToRead);
    numBytesRead += n;
    numBytesToRead -= n;
  }
  readStream.Close();
  writeStream.Close();
}

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mahesh Chand
    0
    Did you check value of Length variable in debug mode?
    0
  • 0
    well i retrieve the value of the response.contentlength (a long int) and it seems to be the neccessary value when beginning the download.  ive noticed that after the download is completed there is some major error in the file size.  eg.

    original file size -> downloaded file size
    11kb  ->  18kb
    66kb  ->  128kb

    ive noticed that these numbers are (double the original - 4kb).  im confused because there is less within the actual zip file but the file sizes are larger??  :S

    hope this helps with a resolution.  Thanks again,
    0
  • 0
    found the problem.  i had a misunderstanding about the stream.read and stream.write parameters.  here is a revised version of the code:

    Byte[] buffer = new Byte[lLength + 5];// + (100 - Length%100) + 5];
    int numBytesRead = 0;
    while (Length > numBytesRead)
    {
      int n;

      n = readStream.Read(buffer, numBytesRead, Length-numBytesRead);
      writeStream.Write(buffer, numBytesRead, n);

      numBytesRead += n;
      tslDLProgressLabel.Text = numBytesRead.ToString() + " bytes / " + Length.ToString() + " bytes";
      tsDLProgress.Value = numBytesRead;
      tsDownload.Refresh();
    }

    Thanks for your help...or lack there of!

    ;) haha jk

    0