如何使用Java解压Mozilla Firefox的jsonlz4会话文件
解决Firefox jsonlz4文件解压问题
错误原因
你遇到的解压报错核心是两个问题:
- 解压缓冲区大小设置不合理:
jsonlz4格式前8字节为固定魔数mozLz40\0,第9~12字节是小端序存储的解压后原始数据的实际长度,你直接设置为压缩长度的2倍大概率无法容纳完整解压后数据,导致解压异常。 - 没有使用文件内置的原始长度参数,依赖经验值估算容量不符合LZ4解压的要求。
修正后的纯Java实现
依赖仍使用lz4-java纯Java实现即可,修正后代码如下:
package com.jsonparser; import net.jpountz.lz4.LZ4Factory; import net.jpountz.lz4.LZ4SafeDecompressor; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; 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[] fileContent = Files.readAllBytes(Paths.get(infile)); // 校验文件格式合法性 String magic = new String(fileContent, 0, 8, StandardCharsets.UTF_8); if (!"mozLz40\0".equals(magic)) { throw new IllegalArgumentException("非合法Firefox jsonlz4格式文件"); } // 读取文件内置的解压后原始长度(小端序4字节) int decompressedSize = ByteBuffer.wrap(fileContent, 8, 4) .order(ByteOrder.LITTLE_ENDIAN) .getInt(); // 提取LZ4压缩块(跳过前12字节) byte[] compressedData = new byte[fileContent.length - 12]; System.arraycopy(fileContent, 12, compressedData, 0, compressedData.length); // 执行解压 LZ4Factory factory = LZ4Factory.fastestInstance(); LZ4SafeDecompressor decompressor = factory.safeDecompressor(); byte[] decompressedBytes = new byte[decompressedSize]; decompressor.decompress(compressedData, 0, compressedData.length, decompressedBytes, 0); // 转换为JSON字符串 String jsonContent = new String(decompressedBytes, StandardCharsets.UTF_8); System.out.println("解压成功,JSON内容:\n" + jsonContent); } }
Maven依赖配置参考:
<dependency> <groupId>org.lz4</groupId> <artifactId>lz4-java</artifactId> <version>1.8.0</version> </dependency>
后续操作说明
解压后得到的是标准JSON结构,可直接用Jackson、Gson等JSON解析库提取所需字段,标签页URL一般存储在windows -> tabs -> entries -> url字段路径下。
内容的提问来源于stack exchange,提问作者localhost
相关产品推荐
相关产品推荐

