How to decompress jsonlz4 files (Firefox bookmark backups) using the command line?
Save this script in a file, e.g., mozlz4
:
#!/usr/bin/env python
from sys import stdin, stdout, argv, stderr
import os
try:
import lz4.block as lz4
except ImportError:
import lz4
stdin = os.fdopen(stdin.fileno(), 'rb')
stdout = os.fdopen(stdout.fileno(), 'wb')
if argv[1:] == ['-c']:
stdout.write(b'mozLz40\0' + lz4.compress(stdin.read()))
elif argv[1:] == ['-d']:
assert stdin.read(8) == b'mozLz40\0'
stdout.write(lz4.decompress(stdin.read()))
else:
stderr.write('Usage: %s -c|-d < infile > outfile\n' % argv[0])
stderr.write('Compress or decompress Mozilla-flavor LZ4 files.\n\n')
stderr.write('Examples:\n')
stderr.write('\t%s -d < infile.json.mozlz4 > outfile.json\n' % argv[0])
stderr.write('\t%s -c < infile.json > outfile.json.mozlz4\n' % argv[0])
exit(1)
I was able to unpack the jsonlz4 by using lz4json
:
apt-get install liblz4-dev
git clone https://github.com/andikleen/lz4json.git
cd lz4json
make
./lz4jsoncat ~/.mozilla/firefox/*/bookmarkbackups/*.jsonlz4
Sufficiently persistent Googling for this turns up a lot of solutions, but most of them seem to be either (a) broken by subsequent changes to underlying libraries, or (b) unnecessarily complex (at least to my personal taste), making them clunky to drop into existing code.
The following appears to work at least on Python 2.7 and 3.6 using a recent version of the Python LZ4 bindings:
def mozlz4_to_text(filepath):
# Given the path to a "mozlz4", "jsonlz4", "baklz4" etc. file,
# return the uncompressed text.
import lz4.block
bytestream = open(filepath, "rb")
bytestream.read(8) # skip past the b"mozLz40\0" header
valid_bytes = bytestream.read()
text = lz4.block.decompress(valid_bytes)
return text
Of course this does not attempt to validate inputs (or outputs), is not intended to be secure, etc., but if one just wants to be able to parse one's own FF data, it gets the basic job done.
Command line version here, which could be saved in the relevant directory and invoked from the command line as:
chmod +x mozlz4.py
./mozlz4.py <file you want to read> <file to save output to>