forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxar.java
More file actions
executable file
·72 lines (64 loc) · 2.05 KB
/
Copy pathxar.java
File metadata and controls
executable file
·72 lines (64 loc) · 2.05 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
68
69
70
71
72
package javaforce.utils;
/** Utility to work with xar files.
*
* INCOMPLETE!!!
*
* Used primarily for Mac OS X installation packages (.pkg).
*
* @author pquiring
*
* Created : May 31, 2014
*/
import java.io.*;
import java.util.zip.*;
import javaforce.*;
public class xar {
private RandomAccessFile raf;
public static void main(String args[]) {
xar i = new xar();
i.open(args[0]);
}
public boolean open(String file) {
try {
raf = new RandomAccessFile(file, "r");
//read header
byte header[] = new byte[28];
if (raf.read(header) != header.length) throw new Exception("bad header:read failed");
if (BE.getuint32(header, 0) != 0x78617221) throw new Exception("bad header:magic");
int header_size = BE.getuint16(header, 4);
if (header_size < header.length) throw new Exception("bad header:too small");
int version = BE.getuint16(header, 6);
long toc_size_compressed = BE.getuint64(header, 8);
long toc_size_uncompressed = BE.getuint64(header, 16);
int cksum_algo = BE.getuint32(header, 24);
if (header_size > header.length) {
raf.seek(header_size);
}
//now read compressed toc
byte toc_compressed[] = new byte[(int)toc_size_compressed];
//TODO : support reading fragments
if (raf.read(toc_compressed) != toc_compressed.length) throw new Exception("bad toc:read failed");
//now decompress toc
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InflaterOutputStream dos = new InflaterOutputStream(baos);
dos.write(toc_compressed);
dos.finish();
byte toc_uncompressed[] = baos.toByteArray();
if (toc_uncompressed.length != toc_size_uncompressed) throw new Exception("bad toc:deflat failed");
System.out.println("toc=" + new String(toc_uncompressed));
} catch (Exception e) {
JFLog.log(e);
return false;
}
return true;
}
public void close() {
if (raf == null) return;
try {
raf.close();
raf = null;
} catch (Exception e) {
JFLog.log(e);
}
}
}