duskwolf Trick Member
Joined: 04 Oct 2006
|
1. Posted: Wed May 02, 2007 8:15 pm Post subject: |
|
|
Here's mine... obviously, this won't help you much if you want a fancy GUI packer, but I guess I might as well release this anyway.
Code: | #!/usr/bin/python
import struct, sys, zlib, os
if len(sys.argv) not in (4, 5):
print "Usage: [python] itgpack.py itg-file.pck input-directory identifier [target size]"
sys.exit(1)
files = []
for dirpath, dirnames, filenames in os.walk(sys.argv[2]):
for filename in filenames:
if filename[0] == '.':
continue # skip dotfiles
abspath = os.path.join(dirpath, filename)
files.append((abspath,
abspath.split(os.path.sep, 1)[1],
os.stat(abspath).st_size))
outf = file(sys.argv[1], "w")
outf.write(struct.pack("< 4s 128s L", "PCKF", sys.argv[3], len(files)))
header_offset = outf.tell()
# Write a preliminary header - leave offsets and sizes zero
print "Scanning .",
for abspath, pckpath, filesize in files:
print ".",
outf.write(struct.pack("<LLLLL", 0, 0, 0, len(pckpath), 0))
outf.write(pckpath)
print
print "Writing files . . ."
offsets = []
# Start writing results
for abspath, pckpath, filesize in files:
data = file(abspath, "r").read()
cdata = zlib.compress(data, 9)[2:]
offset = outf.tell()
outf.write(cdata)
offsets.append((offset, len(cdata)))
print " " + pckpath
eof_offset = outf.tell()
print "Tidying up .",
# Go back and fix the header
outf.seek(header_offset)
for (abspath, pckpath, filesize), (offset, datasize) in zip(files, offsets):
outf.write(struct.pack("<LLLLL",
datasize, filesize, offset, len(pckpath), 1))
outf.write(pckpath)
print ".",
print
if len(sys.argv) == 5:
padlen = int(sys.argv[4])
if padlen < eof_offset:
print "Too much data to hit target file size. Make less."
sys.exit(1)
outf.seek(eof_offset)
padding = padlen - eof_offset
while padding > 0:
padnum = min(padding, 1024)
padding -= padnum
outf.write("\0" * padnum)
print "Written with %d bytes slack space." % (padlen - eof_offset)
print "OK, done. Enjoy." |
|
|