This is the problem:
StreamReader sourceStream = new StreamReader(fileToBeUploaded); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
StreamReader (and any TextReader ) - for text data. Zip file is not text data.
Just use:
byte[] fileContents = File.ReadAllBytes(fileToBeUploaded);
This way you do not treat binary data as text, so it should not be corrupted.
Or, on the contrary, do not load all this into memory separately - just transfer the data:
using (var requestStream = request.GetRequestStream()) { using (var input = File.OpenRead(fileToBeUploaded)) { input.CopyTo(requestStream); } }
Also note that you should use using statements for all of these threads, not just calling Close - this way resources will be deleted even if an exception is thrown.
Jon skeet
source share