java - 如何解压缩 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?
谢谢。
解决方案
jsonlz4
格式是它自己的格式。它基于,lz4
但添加了自己的标题逻辑。因此,它不能被“普通”lz4
解码器解码。
如果您正在寻找一个现成的解码器源代码,lz4
主页列出了一个致力于此目标的项目(尽管它是用 C 编写的):
jsonlz4 解码器,自定义 Mozilla LZ4 格式,由 Avi Halachmi 提供:https ://github.com/avih/dejsonlz4
对于不同的实现,例如使用 Java,您将独自一人。要么在阅读格式的文档后开发自己的标头解码器,然后将剩余的有效负载传递给LZ4
,要么尝试使用 JNI 从 Java 调用 C 函数。
推荐阅读
- java - Mockito 方法存根返回先前测试的存根值
- python - 为什么我的 pygame 代码在 32 位上运行得更快?
- linux - 为什么 gcc 函数属性 noplt 使我的程序变慢?
- c# - 为什么 ViewData 不显示任何内容?
- javascript - 函数的参数在哪里传入
- c - malloc 然后 strcpy 与将其设置为等于字符串之间的区别
- marklogic - MarkLogic:XML 到 JSON 转换中的数组规范
- asp.net - 从 NEST asp.net mvc 中的 elasticsearch 响应中获取亮点
- jquery - 使用ajax重定向到索引
- java - 加载 hdfs 分区文件列表