Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip file is getting corrupted after uploaded to server using C#

Tags:

c#

ftp

dotnetzip

I am trying to upload a zip file to server using C# (Framework 4)and following is my code.

string ftpUrl = ConfigurationManager.AppSettings["ftpAddress"];
string ftpUsername = ConfigurationManager.AppSettings["ftpUsername"];
string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];  
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl + "Transactions.zip");  
request.Proxy = new WebProxy(); //-----The requested FTP command is not supported when using HTTP proxy.
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
StreamReader sourceStream = new StreamReader(fileToBeUploaded);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
            response.Close();  

The zip file is uploaded successfully, but when I tried to open the zip file from server(manually), it showed me Unexpected end of archive error.
For file compression I am using Ionic.zip dll. Before transferring the zip file, I was able to extract successfully.

Any help appreciated. Thanks.

like image 906
Praveen Avatar asked May 11 '13 09:05

Praveen


People also ask

Why do my zip files keep getting corrupted?

ZIP files can get corrupted during the download process. If the download was interrupted, due to a power outage or an unexpected program closure even for a moment, unreadable data can end up becoming part of the downloaded ZIP file and make it difficult for the data to be extracted.

What corrupts a zip file?

Infectious malware can contaminate the zip folder and be transferred across multiple devices, replicating itself. A corrupt hard drive or other storage medium where the ZIP folder is located can corrupt the file as well. Usually occurs when the file or folder is placed at the bad sector of a hard drive.

Can a compressed file be corrupted?

Any compressed archive data is vulnerable to data corruption. Due to various mathematical reasons, you have to have all the compressed data, in order to extract the data that was compressed. There are solutions like PAR and PAR2 but the tools that create those files are no longer developed.


1 Answers

This is the problem:

StreamReader sourceStream = new StreamReader(fileToBeUploaded);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

StreamReader (and any TextReader) is for text data. A zip file isn't text data.

Just use:

byte[] fileContents = File.ReadAllBytes(fileToBeUploaded);

That way you're not treating binary data as text, so it shouldn't get corrupted.

Or alternatively, don't load it all into memory separately - just stream the data:

using (var requestStream = request.GetRequestStream())
{
    using (var input = File.OpenRead(fileToBeUploaded))
    {
        input.CopyTo(requestStream);
    }
}

Also note that you should be using using statements for all of these streams, rather than just calling Close - that way the resources will be disposed even if an exception is thrown.

like image 122
Jon Skeet Avatar answered Sep 19 '22 01:09

Jon Skeet