Skip to content

Commit f340b28

Browse files
author
Jai Prakash
committed
XZ and DeflaterInflater compression is added. | Jai
1 parent e65cda1 commit f340b28

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ dependencies {
1313
testCompile group: 'junit', name: 'junit', version: '4.12'
1414
compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.59'
1515
compile group: 'org.xerial.snappy', name: 'snappy-java', version: '1.1.7.1'
16+
compile group: 'org.tukaani', name: 'xz', version: '1.8'
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package compression;
2+
3+
import sun.misc.BASE64Decoder;
4+
import sun.misc.BASE64Encoder;
5+
6+
import java.util.zip.Deflater;
7+
import java.util.zip.Inflater;
8+
9+
public class DeflaterInflater {
10+
11+
private static int compressedDataLength;
12+
private static final int MAX_BYTE_SIZE = 8192;
13+
14+
public static String compress(String inputString) throws Exception {
15+
byte[] inputBytes = inputString.getBytes("UTF-8");
16+
17+
byte[] compressedData = new byte[MAX_BYTE_SIZE];
18+
Deflater compresser = new Deflater();
19+
compresser.setInput(inputBytes);
20+
compresser.finish();
21+
compressedDataLength = compresser.deflate(compressedData);
22+
compresser.end();
23+
return new BASE64Encoder().encode(compressedData);
24+
}
25+
26+
public static String decompress(String compressedDataString) throws Exception {
27+
byte[] compressedStrBytes = new BASE64Decoder().decodeBuffer(compressedDataString);
28+
byte[] result = new byte[MAX_BYTE_SIZE];
29+
Inflater decompresser = new Inflater();
30+
decompresser.setInput(compressedStrBytes, 0, compressedDataLength);
31+
int resultLength = decompresser.inflate(result);
32+
decompresser.end();
33+
34+
35+
String outputString = new String(result, 0, resultLength, "UTF-8");
36+
return outputString;
37+
38+
}
39+
40+
private static String getFile(String filename) {
41+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
42+
return classLoader.getResource(filename).getPath();
43+
}
44+
}

src/main/java/compression/GZIPCompression.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.zip.GZIPOutputStream;
1010

1111
public class GZIPCompression {
12+
1213
public static String compress(final String str) throws IOException {
1314
if ((str == null) || (str.length() == 0)) {
1415
return null;

src/main/java/compression/Main.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@
66
public class Main {
77
public static void main(String[] args) {
88
try {
9-
System.out.println("Gzip Compression.....");
9+
1010
String fileContent = ReadFile.readTextFile(getFile("sample.txt"));
11+
String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper of "
12+
+ "Snappy, a fast compresser/decompresser.\n Really";
13+
14+
System.out.println("Gzip Compression.....");
1115
String compressedData = GZIPCompression.compress(fileContent);
1216
System.out.println("Compressed: " + compressedData.toString());
1317
String decompressedData = GZIPCompression.decompress(compressedData);
1418
System.out.println("Decompressed: "+decompressedData);
1519

1620
System.out.println("Snappy data compression library.....");
17-
String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper of "
18-
+ "Snappy, a fast compresser/decompresser.\n Really";
1921
byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));
2022
byte[] uncompressed = Snappy.uncompress(compressed);
23+
System.out.println("Snappy compressed: " + compressed);
24+
System.out.println("Snappy decompressed: " + uncompressed);
25+
26+
27+
System.out.println("Deflater Inflater..");
28+
String comp = DeflaterInflater.compress(input);
29+
String uncomp = DeflaterInflater.decompress(comp);
30+
System.out.println("Deflater decompress:" + uncomp);
2131

22-
String result = new String(uncompressed, "UTF-8");
23-
System.out.println(result);
32+
System.out.println("XZ or LZMA2 library.....");
33+
byte[] xzCompressed = XZ_LZMA2.compress(input);
34+
System.out.println(input);
35+
System.out.println("XZ Compressed : "+ xzCompressed);
36+
String xzDecompressed = XZ_LZMA2.decompress(xzCompressed);
37+
System.out.println("XZ Decompressed : "+ xzDecompressed);
2438

2539
}
2640
catch (Exception ex) {
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package compression;
2+
3+
import org.tukaani.xz.LZMA2Options;
4+
import org.tukaani.xz.XZInputStream;
5+
import org.tukaani.xz.XZOutputStream;
6+
7+
import java.io.*;
8+
9+
public class XZ_LZMA2 {
10+
11+
public static byte[] compress(String inputString) throws IOException {
12+
if ((inputString == null) || (inputString.length() == 0)) {
13+
return null;
14+
}
15+
ByteArrayOutputStream xzOutput = new ByteArrayOutputStream();
16+
XZOutputStream xzStream = new XZOutputStream(xzOutput, new LZMA2Options(LZMA2Options.PRESET_MAX));
17+
xzStream.write(inputString.getBytes());
18+
xzStream.close();
19+
return xzOutput.toByteArray();
20+
}
21+
22+
public static String decompress(byte[] compressedData) throws IOException {
23+
if ((compressedData == null) || (compressedData.length == 0)) {
24+
return "";
25+
}
26+
XZInputStream xzInputStream = new XZInputStream(new ByteArrayInputStream(compressedData));
27+
byte firstByte = (byte) xzInputStream.read();
28+
byte[] buffer = new byte[xzInputStream.available()];
29+
buffer[0] = firstByte;
30+
xzInputStream.read(buffer, 1, buffer.length - 2);
31+
xzInputStream.close();
32+
return new String(buffer);
33+
}
34+
}

0 commit comments

Comments
 (0)