Skip to content

Instantly share code, notes, and snippets.

@sjmurdoch sjmurdoch/tarsum forked from guyru/tarsum
Last active Aug 10, 2016

What would you like to do?
A small utility to compute checksums on every file inside a tar archive.
#! /usr/bin/env python3
# Copyright (C) 2008-2009 by Guy Rutenberg
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import hashlib
import tarfile
def tarsum(input_file, hash, output_file):
input_file - A FILE object to read the tar file from.
hash - The name of the hash to use. Must be supported by hashlib.
output_file - A FILE to write the computed signatures to.
tar ="r|*", fileobj=input_file)
chunk_size = 100*1024
store_digests = {}
for member in tar:
if not member.isfile():
f = tar.extractfile(member)
h =
data =
while data:
data =
output_file.write("%s %s\n" % (h.hexdigest(),
def main():
parser = OptionParser()
version=("%prog 0.2.1\n"
"Copyright (C) 2008-2009 Guy Rutenberg <>")
usage=("%prog [options] TARFILE\n"
"Print a checksum signature for every file in TARFILE.\n"
"With no FILE, or when FILE is -, read standard input.")
parser = OptionParser(usage=usage, version=version)
parser.add_option("-c", "--checksum", dest="checksum", type="string",
help="use HASH as for caclculating the checksums. [default: %default]", metavar="HASH",
parser.add_option("-o", "--output", dest="output", type="string",
help="save signatures to FILE.", metavar="FILE")
(option, args) = parser.parse_args()
output_file = sys.stdout
if option.output:
output_file = open(option.output, "w")
input_file = sys.stdin.buffer
if len(args)==1 and args[0]!="-":
input_file = open(args[0], "rb")
tarsum(input_file, option.checksum, output_file)
if __name__ == "__main__":
from optparse import OptionParser
import sys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.