Shitu
Shitu

Reputation: 837

Uncompress .Z file in unix using java

I have a .Z file present on unix box. Is there any way to call unix uncompress command from java?

Upvotes: 6

Views: 3004

Answers (2)

Marian
Marian

Reputation: 140

I also faced the need to decompress .Z archives, looked through internet but have found no better answer than mine below. One can use Apache Commons Compress

FileInputStream fin = new FileInputStream("archive.tar.Z");
BufferedInputStream in = new BufferedInputStream(fin);
FileOutputStream out = new FileOutputStream("archive.tar");
ZCompressorInputStream zIn = new ZCompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = zIn.read(buffer))) {
   out.write(buffer, 0, n);
}
out.close();
zIn.close();

Check this link

This really works

Upvotes: 6

falsetru
falsetru

Reputation: 369054

Using java.lang.Runtime.exec:

Runtime.exec("uncompress " + zFilePath);

UPDATE

Accoridng to the documentation, ProcessBuilder.start() is preferred.

ProcessBuilder pb = new ProcessBuilder("uncompress", zFilePath);
Process p = pb.start();
// p.waitFor();

Upvotes: 0

Related Questions