-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFlashDataDecompressor.java
More file actions
67 lines (61 loc) · 2.37 KB
/
Copy pathFlashDataDecompressor.java
File metadata and controls
67 lines (61 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
public class FlashDataDecompressor {
public static byte[] readFileWithStream(String filePath) throws IOException {
try (FileInputStream fis = new FileInputStream(filePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[8192];
int len;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
return bos.toByteArray();
}
}
public static byte[] decompress(byte[] compressedData, boolean isRawDeflate) throws DataFormatException {
// IsRawDeflate is true, indicating that the data is in raw DEFLATE format
// (without zlib header)
Inflater inflater = new Inflater(isRawDeflate);
inflater.setInput(compressedData);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedData.length);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
outputStream.write(buffer, 0, count);
}
inflater.end();
return outputStream.toByteArray();
}
public static void main(String[] args) {
// Assuming that compressiondData is compressed data received from Flash
// byte[] compressedData = ...;
byte[] compressedData = null;
try {
compressedData = readFileWithStream("0.txt");
// Try decompression in standard zlib format first
byte[] result = decompress(compressedData, false);
System.out.println("Decompression successful (zlib format)"); // run here
System.out.println(new String(result, "GBK"));
} catch (DataFormatException e) {
System.out.println("Standard zlib decompression failed, attempting original DEFLATE format ..");
try {
// If it fails, try decompression in the original DEFLATE format again
byte[] result = decompress(compressedData, true);
System.out.println("Decompression successful (original DEFLATE format)");
System.out.println(new String(result, "GBK"));
} catch (DataFormatException ex) {
System.err.println(
"Decompression failed, the data may be corrupted or an unsupported format may have been used.");
ex.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}