hachoir.editor module

Hachoir editor is a Python library based on Hachoir core used to edit binary files.

Today, only one program uses it: hachoir-strip (remove “useless” information to make a file smaller).

Example: gzip, remove filename

from hachoir.parser import createParser
from hachoir.editor import createEditor
from hachoir.field import writeIntoFile

parser = createParser("file.gz")
with parser:
    editor = createEditor(parser)
    del editor["filename"]
    editor["has_filename"].value = False
    writeIntoFile(editor, "noname.gz")

Example: gzip, add extra

from hachoir.parser import createParser
from hachoir.editor import createEditor
from hachoir.field import writeIntoFile
from hachoir.editor import EditableInteger, EditableBytes

parser = createParser("file.gz")
with parser:
    editor = createEditor(parser)
    extra = "abcd"
    editor["has_extra"].value = True
    editor.insertAfter("os",
                       EditableInteger(editor, "extra_length", False,
                                       16, len(extra)),
                       EditableBytes(editor, "extra", extra))
    writeIntoFile(editor, "file_extra.gz")

Example: zip, set comment

from hachoir.parser import createParser
from hachoir.editor import createEditor
from hachoir.field import writeIntoFile

parser = createParser("file.zip")
with parser:
    editor = createEditor(parser)
    editor["end_central_directory/comment"].value = "new comment"
    writeIntoFile(editor, "file_comment.zip")