Is there a Python library that packs binary data into one file?

Discussion in 'Python' started by Jacob H, Apr 26, 2004.

  1. Jacob H

    Jacob H Guest

    Hello all,

    Today I began writing a utility script that takes given binary files
    and puts them all into one datafile. My idea is to be able to access
    any binary data I want by indexing the datafile, e.g.
    wanted_image_data = datafileobj[IMAGE_DATA]. The purpose is to hide
    external image files from the user in a simple game I'm writing.

    Though I have a good idea of how to implement this, before I begin I
    am curious to know if some Python master out there has already come
    out with a library that does the same. Anyone? :)
     
    Jacob H, Apr 26, 2004
    #1
    1. Advertising

  2. Jacob H

    John Hunter Guest

    >>>>> "Jacob" == Jacob H <> writes:

    Jacob> Hello all, Today I began writing a utility script that
    Jacob> takes given binary files and puts them all into one
    Jacob> datafile. My idea is to be able to access any binary data I
    Jacob> want by indexing the datafile, e.g. wanted_image_data =
    Jacob> datafileobj[IMAGE_DATA]. The purpose is to hide external
    Jacob> image files from the user in a simple game I'm writing.

    Jacob> Though I have a good idea of how to implement this, before
    Jacob> I begin I am curious to know if some Python master out
    Jacob> there has already come out with a library that does the
    Jacob> same. Anyone? :) --

    How about putting it into a tar file? There are many archive file
    formats (ISO9960, tar, zip). You could just reuse one of these and an
    existing python interface to them, compressing and encrypting as
    necessary if you need extra obscurity.

    JDH
     
    John Hunter, Apr 26, 2004
    #2
    1. Advertising

  3. Jacob H

    Larry Bates Guest

    zipfile.py works pretty well and you get compression
    as well.

    Larry Bates
    Syscon, Inc.

    "Jacob H" <> wrote in message
    news:...
    > Hello all,
    >
    > Today I began writing a utility script that takes given binary files
    > and puts them all into one datafile. My idea is to be able to access
    > any binary data I want by indexing the datafile, e.g.
    > wanted_image_data = datafileobj[IMAGE_DATA]. The purpose is to hide
    > external image files from the user in a simple game I'm writing.
    >
    > Though I have a good idea of how to implement this, before I begin I
    > am curious to know if some Python master out there has already come
    > out with a library that does the same. Anyone? :)
     
    Larry Bates, Apr 26, 2004
    #3
  4. Jacob H

    Max M Guest

    Jacob H wrote:

    > Hello all,
    >
    > Today I began writing a utility script that takes given binary files
    > and puts them all into one datafile. My idea is to be able to access
    > any binary data I want by indexing the datafile, e.g.
    > wanted_image_data = datafileobj[IMAGE_DATA]. The purpose is to hide
    > external image files from the user in a simple game I'm writing.



    What you need, is a simple pickle of a dictionary save to disk.

    It's dead easy, and does exactly what you want.

    import cPickle

    some_dict = {'my':'name','is':'norman','bates':'!'}
    file_name = 'some.dict'

    f = open(file_name, 'wb')
    cPickle.dump(some_dict, f, -1)
    f.close()

    f = open(file_name, 'rb')
    c = cPickle.load(f)
    f.close()

    print c

    >>{'my': 'name', 'is': 'norman', 'bates': '!'}



    "
    3.14 pickle -- Python object serialization

    The pickle module implements a fundamental, but powerful algorithm for
    serializing and de-serializing a Python object structure. ``Pickling''
    is the process whereby a Python object hierarchy is converted into a
    byte stream, and ``unpickling'' is the inverse operation, whereby a byte
    stream is converted back into an object hierarchy. Pickling (and
    unpickling) is alternatively known as ``serialization'',
    ``marshalling,''3.2 or ``flattening'', however, to avoid confusion, the
    terms used here are ``pickling'' and ``unpickling''.

    This documentation describes both the pickle module and the cPickle module.
    "



    regards Max M
     
    Max M, Apr 26, 2004
    #4
  5. Jacob H wrote:

    >Hello all,
    >
    >Today I began writing a utility script that takes given binary files
    >and puts them all into one datafile. My idea is to be able to access
    >any binary data I want by indexing the datafile, e.g.
    >wanted_image_data = datafileobj[IMAGE_DATA]. The purpose is to hide
    >external image files from the user in a simple game I'm writing.
    >
    >Though I have a good idea of how to implement this, before I begin I
    >am curious to know if some Python master out there has already come
    >out with a library that does the same. Anyone? :)
    >
    >

    Not quite what you're asking for, but ResourcePackage embeds resources
    automatically in Python files. If you are py2exe'ing your projects the
    result is that the images are invisible (they are part of the Python
    bytecode files py2exe packs into the exe). The nice part is the
    automation; you just update the data-file (stored in a sub-package of
    your main game package) and the embedded version is automagically
    updated on next import of the resource package (run of the game).

    http://resourcepackage.sf.net/

    Have fun,
    Mike

    _______________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://members.rogers.com/mcfletch/
     
    Mike C. Fletcher, Apr 26, 2004
    #5
  6. Jacob H

    Brian Kelley Guest

    Jacob H wrote:
    > Hello all,
    >
    > Today I began writing a utility script that takes given binary files
    > and puts them all into one datafile. My idea is to be able to access
    > any binary data I want by indexing the datafile, e.g.
    > wanted_image_data = datafileobj[IMAGE_DATA]. The purpose is to hide
    > external image files from the user in a simple game I'm writing.


    In the vein of giving a man a fish: if you are using python 2.2+

    import dbhash, zlib

    db = dbhash.open("foo.db", 'w')

    db['hi'] = zlib.compress("my dog has fleas")
    print zlib.decompress(db['hi'])

    for more help
    type

    help(dbhash)

    at the interpreter

    In the vein of teaching a man to fish, read the library reference here
    for a lot of python goodies:

    http://python.org/doc/2.3.3/lib/

    Brian
     
    Brian Kelley, Apr 27, 2004
    #6
  7. Larry Bates wrote:

    > zipfile.py works pretty well and you get compression
    > as well.
    >


    If you're after better compression there's also bzip2...


    --

    ******************************************************************************
    Registered Linux User Number 185956
    http://groups.google.com/groups?hl=en&safe=off&group=linux
    Join me in chat at #linux-users on irc.freenode.net
    Buy an Xbox for $149.00, run linux on it and Microsoft loses $150.00!
    9:22pm up 14 days, 43 min, 5 users, load average: 3.05, 3.06, 2.99
     
    Jerry McBride, May 4, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UU5ndXllbg==?=

    Error loading webform after applying service packs

    =?Utf-8?B?UU5ndXllbg==?=, Oct 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    329
    =?Utf-8?B?UU5ndXllbg==?=
    Oct 21, 2004
  2. Great Job
    Replies:
    0
    Views:
    422
    Great Job
    Feb 9, 2008
  3. Scott M.

    ASP .NET AJAX in Service Packs

    Scott M., Mar 15, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    271
    bruce barker
    Mar 16, 2009
  4. Baba
    Replies:
    71
    Views:
    2,818
  5. Hrvoje
    Replies:
    0
    Views:
    827
    Hrvoje
    Sep 30, 2010
Loading...

Share This Page