开发者

Is there a way to check whether the file I'm accessing using Ruby is a completed downloaded file?

I want to access the files in a folder using the ruby co开发者_JAVA百科de. But these files will be downloaded into the folder then and there. so new files will be downloaded every time. But when I access any of these files, how can I know whether the file is completely downloaded and not a partial file. Is there a way to find it in Ruby programming?


Use flock to obtain an exclusive lock on the file. If it can't be obtained, something else could be writing to it. I'm not 100% sure how this works on all file systems / OSes.

You could also poll the file every few seconds to see if the size is changing, but that seems quite hacky.


Polling file size for any changes is indeed somewhat hacky, but seems to be an approach that is used quite a lot.

Alternatively, do you have any control over the way files are downloaded? If so, some sort of additional "stub" file might be the answer:

  1. The main requested file is downloaded (e.g. "MyFile.zip"). When the request is fulfilled at the server end, chain another file download after it--e.g. "MyFile.follow" (Note that this doesn't have to be a complete file, it can just be a "stub" of some sort).

  2. For each downloaded file, your Ruby code checks for the presence of its accompanying "follow" file. If it's there, assume the downloaded file is complete.

Of course, this is not fool-proof, but short of checking downloaded file sizes against some sort of table of "known" file sizes, I'm not sure what else you could do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜