473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Comparing files in a zip to files on drive

I am attempting to incrementally back up files using python. How can I
compare a file on a hard drive to a file in a python created zip file
to tell if the file has changed? Or should I be using some other
method to determine if a file has changed?

Dec 29 '06 #1
2 3431
kj7ny wrote:
compare a file on a hard drive to a file in a python created zip file
to tell if the file has changed?
>>fileondisk = r'C:\Documents and Settings\power\Desktop\ES3dc+Template.3f'
zipondisk = r'C:\Documents and Settings\power\Desktop\temps.zip'
import zipfile
z = zipfile.ZipFile(zipondisk)
z.namelist()
['ES1+Template.3f', 'ES3sc+Template.3f', 'ES3dc+Template.3f']
>>info = z.getinfo('ES3dc+Template.3f')
info.CRC
1620938006
>>import binascii
checksum = 0
fp = open(fileondisk, 'rb')
while 1:
.... data = fp.read(32*1024)
.... if not data: break
.... checksum = binascii.crc32(data, checksum)
....
>>checksum
1620938006
>>>
Dec 29 '06 #2
kj7ny wrote:
I am attempting to incrementally back up files using python. How can I
compare a file on a hard drive to a file in a python created zip file
to tell if the file has changed? Or should I be using some other
method to determine if a file has changed?
You can easily determine size and date from the hard drive/filesystem
and from zip files. Look at the Python docs for the os and zipfile
modules. That would be the fastest way to detect changes for most files
you will encounter.

Zip files have the CRC of each file (generated from the original
uncompressed file) for checksumming purposes. You can calculate the CRC
for the file on the hard drive and then compare the CRC values.
Calculating the CRC should be faster than decompressing the ZIPped file
or compressing the file on the hard drive. While not perfect it should
definitely give you a very good indication of changed content. Python
doesn't any built-in functions for CRC calculations, AFAIK. There are
third party modules around.

A better plan would be to store a list of MD5 or SHA1 hashes for each
file in your archive. These functions are built in to Python and are
more suitable for detection large differences in files (these are hash
functions while CRC is a checksum function and thus are less likely to
have collisions). You can store the list of hashes in a separate file,
or as a file in the ZIP file, or even in the ZIP file header.

Your last resort would be to decompress each file and compare it with
the one on disk. This will take a lot of time and memory. Not at all
advisable but definitely foolproof.

There are other issues you will have to contend with, such as renaming
of files and directories, and deletions. If I were you, I'd take a look
at the work done on programs like rsync, unison (both file
synchronization programs), or CVS and Subversion (both version or source
control programs) to see how they deal with such issues.
Dec 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dan Stromberg | last post by:
We will soon have 3 copies, for testing purposes, of what should be about 4.5 terrabytes of data. Rather than cmp'ing twice, to verify data integrity, I was thinking we could speed up the...
2
by: Domenico Discepola | last post by:
Hello all. Before my arrival at my current employer, our consultants physically set up our MSSQL 7 server as follows: drive c: contains the mssql engine drive d: contains the transaction log...
8
by: Claudio Grondi | last post by:
It is maybe not a pure Python question, but I think it is the right newsgroup to ask for help, anyway. After connecting a drive to the system (via USB or IDE) I would like to be able to see...
2
by: Rudy | last post by:
Hello all! I have a web application, that I can upload files to a folder from the web site. The file path is stored in SQL. I have a 73GB drive, external hooked into the web server. I can load up...
4
by: BeeF | last post by:
Hi Guys, Help out a new user here I want to compare a directory on my drive with files on a web page. So my problem realy has two parts first the comparison. I'm thinking of loading the files...
18
by: Joe Lester | last post by:
This thread was renamed. It used to be: "shared_buffers Question". The old thread kind of died out. I'm hoping to get some more direction by rephrasing the problem, along with some extra...
4
by: sajid_yusuf | last post by:
Hi I am trying to develop a Windows service in VB.NET which has timer enabled and keeps checking a folder (or group of folders) for any new file or changed files. As soon as it detects any new...
2
by: athos | last post by:
OS: Windows 2000/XP (needs to run on 2 different machines) Language: Python 2.5 Programmer Level: Pathetically new to Python Goal: Using code I've altered for my needs, I'm attempting to create...
9
by: NvrBst | last post by:
Whats the best way to count the lines? I'm using the following code at the moment: public long GetNumberOfLines(string fileName) { int buffSize = 65536; int streamSize = 65536; long...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.