0

I received a file, I need to compress this file and pass the file name of compressed file as an argument to another method.

File fileName = new File("fileA");

How can I snappy compress this file and get the file name of the compressed file?

Thanks!

NoName
  • 1,509
  • 2
  • 20
  • 36

1 Answers1

0

You can use this library: Snappy for java

First read the file to a String, then compress it:

String input = FileUtils.readFileToByteArray(new File("fileA"));

byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));

String result = new String(uncompressed, "UTF-8");

Finally write it to a file of your choice:

FileUtils.writeStringToFile(new File("targetFile"),result,"UTF-8")

I have used Apache Common FileUtils for brevity here.

Victor
  • 2,450
  • 2
  • 23
  • 54