I've been trying to decode object streams encoded with Flatedecode using the following code.
public class test2 {
public static byte[] decodeStream(byte[] stream) {
Inflater inflater = new Inflater();
inflater.setInput(stream);
byte[] decoded = new byte[1024];
int result = 0;
try {
result = inflater.inflate(decoded);
} catch (DataFormatException e) {
e.printStackTrace();
}
return result > 0 ? decoded : null;
}
public static void findstream(String text) {
Pattern pattern = Pattern.compile("\\n<<[^\r\n]+>>stream\\r\\n");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
String matched = matcher.group();
int offset = matcher.end();
int length = Integer.parseInt(matched.trim().split("\\s")[1].split("/")[0]);
byte[] compressed = text.substring(offset, offset + length).getBytes();
System.out.println(new String(decodeStream(compressed)));
}
}
public static void main(String arg[]) {
File file = new File("F:\\users\\public\\Downloads", "P887906-01.pdf");
if (!file.exists()) {
System.out.println("File not found");
System.exit(0);
}
FileInputStream in = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (in == null) System.exit(0);
byte[] buffer = new byte[4096];
try {
int count = in.read(buffer);
if (count > 0) findstream(new String(buffer));
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
As I understand, the problem is with the dictionary, since the needDictionary is set to true after i used the inflate method, but i really don't know how to fix it. Please help me understand what i'm doing wrong.