比较本地文件和远程文件 [英] Comparing local file with remote file

查看:109
本文介绍了比较本地文件和远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:我在服务器上有一个本地.zip文件和一个.zip文件.我需要检查服务器上的.zip文件是否不同于本地文件.如果不是,我需要从服务器中拉出新的.我的问题是如何比较它们而不从服务器下载文件并在本地进行比较?

I have the following problem: I have a local .zip file and a .zip file located on a server. I need to check if the .zip file on the server is different from the local one; if they are not I need to pull the new one from the server. My question is how do I compare them without downloading the file from the server and comparing them locally?

创建.zip文件时,我可以在服务器上为zip文件创建MD5哈希,然后将其与本地.zip文件的MD5进行比较,但是有没有更简单的方法?

I could create an MD5 hash for the zip file on the server when creating the .zip file and then compare it with the MD5 of my local .zip file, but is there a simpler way?

推荐答案

简短的回答:不能.

长答案:要与服务器上的zip文件进行比较,必须读取该文件.您可以在本地执行此操作(这涉及将其拉出),也可以要求服务器为您执行此操作.您可以在服务器上运行代码吗?

Long answer: To compare with the zip file on the server, someone has to read that file. Either you can do that locally, which would involve pulling it, or you can ask the server to do it for you. Can you run code on the server?

如果您可以在服务器上运行Python,为什么不对文件进行哈希处理并比较哈希呢?

If you can run Python on the server, why not hash the file and compare hashes?

import hashlib
with open( <path-to-file>, "rb" ) as theFile:
    m = hashlib.md5( )
    for line in theFile:
        m.update( line )
with open( <path-to-hashfile>, "wb" ) as theFile:
    theFile.write( m.digest( ) )

,然后将hashfile的内容与本地生成的哈希进行比较?

and then compare the contents of hashfile with a locally-generated hash?

您要求一种更简单的方法.暂时以抽象的方式考虑一下:

You asked for a simpler way. Think about this in an abstract way for a moment:

  • 您不想下载整个zip文件.
  • 因此,您无法在本地处理整个文件(因为这将涉及从服务器读取所有文件,这相当于下载它!).
  • 因此,您需要在服务器上进行一些处理.具体来说,您想提供一些少量的数据来对文件进行编码",以便无需获取整个文件就可以获取少量的数据.
  • 但这是哈希!

因此,您需要进行某种哈希处理.鉴于此,我认为以上内容非常简单.

Therefore, you need to do some sort of hashing. Given that, I think the above is pretty simple.

这篇关于比较本地文件和远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆