Eric Anastas
Eric Anastas

Reputation: 22233

How can I tell if a file on an FTP is identical to a local file with out actually downloading the file?

I'm writing a simple program that is used to synchronize files to an FTP. I want to be able to check if the local version of a file is different from the remote version, so I can tell if the file(s) need to be transfered. I could check the file size, but that's not 100% reliable because obviously it's possible for two files to be the same size but contain different data. The date/time the files were modified is also not reliable as the user's computer date could be set wrong.

Is there some other way to tell if a local file and a file on an FTP are identical?

Upvotes: 7

Views: 14677

Answers (6)

Despertar
Despertar

Reputation: 22392

Whenever your client uploads files to the FTP server, map each file to its hash and store it locally on the client computer (or store it anywhere you can access later, format doesnt matter, can be an xml file, plain text, as long as you can retreive the key/value pairs). Then when you upload files again just check the local files with the hash table you created, if it is different then upload the file. This way you don't have to rely on the server to maintain a checksum file and you dont have to have a process running to monitor the FileSystemWatcher events.

Upvotes: 0

Chris Hayes
Chris Hayes

Reputation: 4033

Couldn't you use a FileSystemWatcher and just have the client remember what changed? http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

Upvotes: 0

Lex Li
Lex Li

Reputation: 63295

IETF tried to achieve this by adding new FTP commands such as MD5 and MMD5.

http://www.faqs.org/rfcs/ftp-rfcs.html

However, no all FTP vendors support them. So you must have a check on the targeting FTP server you application will work against to see if it supports MD5/MMD5. If not, you can pick up the workarounds mentioned above.

Upvotes: 1

Tim Sylvester
Tim Sylvester

Reputation: 23168

If the server is plain-old FTP, you can't do any better than checking the size and timestamps.

FTP has no mechanism for giving you the hashes/checksums of files, so you would need to do something like keeping a special "listing file" that has all the file names and hashes, or doing a separate request via HTTP, or some other protocol.

Ideally, you should not be using FTP anyway, it's really an obsolete protocol. If you have control of the system, you could use rsync or something like it.

Upvotes: 2

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76611

There isn't a generic way. If the ftp site includes a checksum file, you can download that (which will be a lot quicker since a checksum is quite small) and then see if the checksums match. But of course, this relies on the owner of the ftp site creating a checksum file and keeping it up to date.

Other then that, you are S.O.L.

Upvotes: 5

Jim Deville
Jim Deville

Reputation: 10672

Use a checksum. You generate the md5 (or sha1, sha2 etc) hash of both files, and if the files are identical, then the hashes will be identical.

Upvotes: 1

Related Questions