首页 > 解决方案 > 如何解压缩 jsonlz4 Mozilla Firefox 会话文件?

问题描述

我正在尝试jsonlz4从 Firefox 解码包含我保存的选项卡的文件(保存在%appdata%\Mozilla\Firefox\Profiles\xxxxxx.default-release\sessionstore-backups\recovery.jsonlz4中,希望最终能够解析 json 并提取选项卡中的 URL 以及会话中的其他数据。

我希望该lz4-pure-java库可以将其解压缩为 json。

我正在尝试使用来自lz4-java github 的方法 2 示例,这里的评论说如果我们跳过 12 字节的标头,该文件应该是标准的 lz4。

这是我的代码:

package com.jsonparser;

import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4SafeDecompressor;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

public class jsonLZ4 {
    public static void main(String[] args) throws IOException {

        String infile = "C:\\Users\\username\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\xxxxxx.default-release\\sessionstore-backups\\recovery.jsonlz4";

        byte[] datain = Files.readAllBytes(Paths.get(infile));

        // need to skip the first 12 bytes for Firefox format

        byte[] data = Arrays.copyOfRange(datain,12,datain.length);

        LZ4Factory factory = LZ4Factory.fastestInstance();

        byte[] compressed = data;
        int compressedLength = compressed.length;
        byte[] restored = new byte[compressed.length*2]; // not sure how to set length properly without knowing decompressed size

        // - method 2: when the compressed length is known (a little slower)
        // the destination buffer needs to be over-sized

        LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
        int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);

        String s = new String(restored, StandardCharsets.UTF_8);
        System.out.println("decompressed data is: " + s);
    }
}

不幸的是,我收到了一个解压错误:

Exception in thread "main" net.jpountz.lz4.LZ4Exception: Malformed input at 1803197
    at net.jpountz.lz4.LZ4JavaUnsafeSafeDecompressor.decompress(LZ4JavaUnsafeSafeDecompressor.java:62)
    at net.jpountz.lz4.LZ4SafeDecompressor.decompress(LZ4SafeDecompressor.java:77)
    at com.jsonparser.jsonLZ4.main(jsonLZ4.java:31)

Process finished with exit code 1

有谁知道我怎样才能成功解压这个文件,最好只使用java?

谢谢。

标签: javajsonlz4

解决方案


jsonlz4格式是它自己的格式。它基于lz4但添加了自己的标题逻辑。因此,它不能被“普通”lz4解码器解码。

如果您正在寻找一个现成的解码器源代码,lz4主页列出了一个致力于此目标的项目(尽管它是用 C 编写的):

jsonlz4 解码器,自定义 Mozilla LZ4 格式,由 Avi Halachmi 提供:https ://github.com/avih/dejsonlz4

对于不同的实现,例如使用 Java,您将独自一人。要么在阅读格式的文档后开发自己的标头解码器,然后将剩余的有效负载传递给LZ4,要么尝试使用 JNI 从 Java 调用 C 函数。


推荐阅读