A while back I asked a question revolving around how to copy a file in chunks from one location to another: CopyFileEx "The parameter is invalid" error
I received the following code which was quite helpful.
static void chunkCopyFile(string source, string destination, int bytesPerChunk)
{
uint bytesRead = 0;
using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read)) {
using (BinaryReader br = new BinaryReader(fs)) {
using (FileStream fsDest = new FileStream(destination, FileMode.Create)) {
BinaryWriter bw = new BinaryWriter(fsDest);
byte[] buffer;
for (int i = 0; i < fs.Length; i += bytesPerChunk) {
buffer = br.ReadBytes(bytesPerChunk);
bw.Write(buffer);
bytesRead += Convert.ToUInt32(bytesPerChunk);
updateProgress(bytesRead);
}
}
}
}
}
However, I now need to convert this code to use FTP instead. I tried the obvious of just passing the FTP path to the filestream but it gave me an error saying "unsupported".
I already managed to get the file length, I'm just not sure how I can split the download into chunks. Any help is appreciated as always!
Code so far(not much)
static void chunkCopyFTPFile(string destination, int bytesPerChunk)
{
uint bytesRead = 0;
fWR = (FtpWebRequest)WebRequest.Create("ftp://" + FTP_SERVER_NAME + "/test.txt");
fWR.Method = WebRequestMethods.Ftp.DownloadFile;
fWR.UseBinary = true;
fWR.Credentials = new NetworkCredential(FTP_SERVER_USERNAME, FTP_SERVER_PASSWORD);
FtpWebResponse response = (FtpWebResponse)fWR.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader sR = new StreamReader(responseStream);
sR.ReadToEnd();
sR.Close();
response.Close();
}
Final code (working):
using (Stream responseStream = response.GetResponseStream()) {
using (BinaryReader bR = new BinaryReader(responseStream)) {
using (FileStream fsDest = new FileStream(destination, FileMode.Create)) {
BinaryWriter bw = new BinaryWriter(fsDest);
int readCount;
byte[] buffer = new byte[bytesPerChunk];
readCount = responseStream.Read(buffer, 0, bytesPerChunk);
bytesRead += Convert.ToUInt32(readCount);
updateProgress(bytesRead);
while (readCount > 0) {
bw.Write(buffer, 0, readCount);
readCount = responseStream.Read(buffer, 0, bytesPerChunk);
bytesRead += Convert.ToUInt32(readCount);
updateProgress(bytesRead);
}
}
}
}