I have tried to gzip a large(100mb to 500mb) xml file.I have created method Zip to do that. the issues is that its talking too much time to zip.for 200mb it take 1.2 secs.i need to reduce the time too 100 millisecond for 100mb xml file. how do i optimize to reduce the time for zipping?
I have reduced the time by compromising little on compression ratio. tried another algorithms like Snappy,Lz4 but not much improvement and also they have poor compression.as per my knowledge gzipOutputStream.write() takes 85% of the time.so how can i optimize this step to get better performance with out compromising much of compression Ratio.
public static String zip(final String str) {
if ((str == null) || (str.length() == 0)) {
throw new IllegalArgumentException("Cannot zip null or empty string");
}
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(str.length())) {
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream){{def.setLevel(Deflater.BEST_SPEED );}};) {
gzipOutputStream.write(str.getBytes(StandardCharsets.UTF_8));
}
T5 = System.currentTimeMillis();
byte[] bytes=byteArrayOutputStream.toByteArray();
T3 = System.currentTimeMillis();
String zipped_text=DatatypeConverter.printBase64Binary(bytes);
T4 = System.currentTimeMillis();
return zipped_text;
} catch(IOException e) {
throw new RuntimeException("Failed to zip content", e);
}
}