比较本地文件与远程文件

发布于 2024-09-13 08:17:58 字数 272 浏览 4 评论 0原文

我遇到以下问题:我有一个本地 .zip 文件和一个位于服务器上的 .zip 文件。我需要检查服务器上的.zip文件是否与本地不同;如果不是,我需要从服务器中提取新的。我的问题是如何在不从服务器下载文件并在本地比较它们的情况下比较它们?

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

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十二 2024-09-20 08:17:58

简短的回答:你不能。

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

编辑

如果可以在服务器上运行Python,为什么不对文件进行散列并比较散列呢?

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 的内容与本地生成的哈希进行比较?

另一种编辑

您要求一种更简单的方法。以抽象的方式思考一下:

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

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

Short answer: You can't.

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?

Edit

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( ) )

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

Another edit

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

  • You don't want to download the entire zip file.
  • Hence, you can't process the entire file locally (because that would involve reading all of it from the server, which is equivalent to downloading it!).
  • Hence, you need to do some processing on the server. Specifically, you want to come up with some small amount of data that 'encodes' the file, so that you can fetch this small amount of data without fetching the whole file.
  • But this is a hash!

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

瑕疵 2024-09-20 08:17:58

我想知道如果是这样的话,您打算如何在本地比较它们。您可以应用相同的逻辑来远程比较它们。

I would like to know how you intend to compare them locally, if it were the case. You can apply the same logic to compare them remotely.

这个俗人 2024-09-20 08:17:58

您可以使用 ssh 登录并为远程文件生成 md5 哈希值,并为当前本地文件生成 md5 哈希值。如果 md5 匹配,则文件相同,否则文件不同。

You can log in using ssh and make a md5 hash for the file remotely and a md5 hash for the current local file. If the md5s are matching the files are identicaly, else they are different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文