Expand .Z file on unix with java

I have a .Z file present in a unix block. Is it possible to invoke the unix uncompress command from java?

+4
source share
2 answers

Using java.lang.Runtime.exec:

Runtime.exec("uncompress " + zFilePath);

UPDATE

According to the documentation is preferable ProcessBuilder.start().

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

I also encountered the need to unzip .Z archives, browsing the Internet, but could not find a better answer than mine. You can use Apache Commons compression

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

It really works

+2
source

Source: https://habr.com/ru/post/1533901/


All Articles