@@ -7,19 +7,21 @@ This library provides LZMA compression for applications that run on the Java pla
7
7
## Background ##
8
8
9
9
This library is based on the [ Java LZMA SDK] ( http://www.7-zip.org/sdk.html ) by Igor Pavlov.
10
+ It provides some deserved enhancements.
10
11
11
- While the code works just fine, any potential user will quickly realize that :
12
+ While the original code works just fine, it has some serious issues for Java developers :
12
13
13
- * this is a straight port of non-object procedural code written in C to Java, and
14
+ * this is a straight port of non-object, procedural code written in C to Java, and
14
15
* the code does not follow Java conventions (e.g., methods names start by
15
16
capital letters)
16
17
* configuration of the LZMA encoder and decoders require passing around
17
18
arrays and numbers for which no proper documentation or constants exists
18
19
other than source code, and
19
- * ...there is no stream api to plug into java.io streams.
20
+ * ...there is no stream api to plug into ` java.io ` streams.
20
21
21
- There is unfortunately no public description of the LZMA algorithms, so a
22
- rewrite was clearly a hard task. I decided to create this library as follows.
22
+ There is unfortunately no public description of the LZMA algorithms other than source code,
23
+ so a rewrite was clearly a hard task. I decided to create this library using the following
24
+ methodology.
23
25
24
26
1 . Import the Java LZMA SDK code.
25
27
2 . Convert methods and package names to Java conventions.
@@ -28,13 +30,69 @@ rewrite was clearly a hard task. I decided to create this library as follows.
28
30
5 . Run static code analysis to clean the code (unused variables, unusued parameters,
29
31
unused methods, expressions simplifications and more).
30
32
6 . Do some profiling.
31
- 7 . Build a streaming api that would fit into java.io streams.
33
+ 7 . Build a streaming api that would fit into ` java.io ` streams.
34
+ 8 . Provide some higher-level abstractions to the LZMA encoder / decoders configuration.
32
35
33
36
Although not a derivate work, the streaming api classes were inspired from
34
37
[ the work of Christopher League] ( http://contrapunctus.net/league/haques/lzmajio/ ) . I reused
35
38
his technique of fake streams and working threads to pass the data around between
36
39
encoders/decoders and "normal" Java streams.
37
40
41
+ ## Usage ##
42
+
43
+ There are two main Java package hierarchies:
44
+
45
+ * ` lzma.sdk ` is the (reworked) Java LZMA SDK code, and
46
+ * ` lzma.streams ` contains the ` LzmaInputStream ` and ` LzmaInputStream ` classes.
47
+
48
+ You will probably only be interested in using the ` lzma.streams ` package. The two
49
+ stream classes use the good practices of constructor dependency injection, and you will
50
+ need to pass them the decorated streams and LZMA encoder / decoders from the SDK.
51
+
52
+ You can simply instanciate a ` Decoder ` and pass it to the constructor of ` LzmaInputStream `
53
+ without specifying further configuration: it will read it from the input stream.
54
+
55
+ The ` Encoder ` class that ` LzmaOutputStream ` depends on needs some configuration. You can
56
+ either do it manually (checkout the ` Encoder ` class to guess what those integer values mean!),
57
+ or you can use the ` LzmaOutputStream.Builder ` class which makes it much easier to configure.
58
+
59
+ The following code is from a unit test. It should make the basic usage of the library relatively
60
+ obvious:
61
+
62
+ public void test_round_trip() throws IOException
63
+ {
64
+ final File sourceFile = new File("LICENSE");
65
+ final File compressed = File.createTempFile("lzma-java", "compressed");
66
+ final File unCompressed = File.createTempFile("lzma-java", "uncompressed");
67
+
68
+ final LzmaOutputStream compressedOut = new LzmaOutputStream.Builder(
69
+ new BufferedOutputStream(new FileOutputStream(compressed)))
70
+ .useMaximalDictionarySize()
71
+ .useEndMarkerMode(true)
72
+ .useBT4MatchFinder()
73
+ .build();
74
+
75
+ final InputStream sourceIn = new BufferedInputStream(new FileInputStream(sourceFile));
76
+
77
+ copy(sourceIn, compressedOut);
78
+ sourceIn.close();
79
+ compressedOut.close();
80
+
81
+ final LzmaInputStream compressedIn = new LzmaInputStream(
82
+ new BufferedInputStream(new FileInputStream(compressed)),
83
+ new Decoder());
84
+
85
+ final OutputStream uncompressedOut = new BufferedOutputStream(
86
+ new FileOutputStream(unCompressed));
87
+
88
+ copy(compressedIn, uncompressedOut);
89
+ compressedIn.close();
90
+ uncompressedOut.close();
91
+
92
+ assertTrue(contentEquals(sourceFile, unCompressed));
93
+ assertFalse(contentEquals(sourceFile, compressed));
94
+ }
95
+
38
96
## License ##
39
97
40
98
The LZMA SDK is in the public domain. I relicensed the whole under the liberal
0 commit comments