问题
Do anyone have coding in java to extract .tar.Z files.
Assume that i am having file called home.tar.Z I need extract this file using java code, can you give any sample code.
Thanks
回答1:
The .Z
extension is not the gzip
extension, it is from compress
.
You can launch shell scripts from Java, I'd do it like that, since I don't know any compress
library for Java. For tar/gz you could use Plexus Archiver or Apache Commons VFS.
See
- How do I extract a tar file in Java?
- How to Compress/Decompress tar.gz files in java
Sadly it doesn't seem that any libraries cover .Z
files, so I think those links are unusable for you.
But take a look at:
- How to make pipes work with Runtime.exec()?
This allows you to do everything with the shell commands which means you won't have to bother doing it from Java.
回答2:
It is obviously late to help the OP, but for posterity, Apache commons-compress does support *.Z.
For example
import org.apache.commons.compress.compressors.z.ZCompressorInputStream;
...
FileInputStream fin = new FileInputStream("/path/to/archive.something.Z");
BufferedInputStream in = new BufferedInputStream(fin);
ZCompressorInputStream zIn = new ZCompressorInputStream(in);
BufferedReader br = new BufferedReader(new InputStreamReader(zIn));
while (null != (content = br.readLine())) {
System.out.println(content);
}
br.close();
来源:https://stackoverflow.com/questions/15039566/i-need-to-extract-tar-z-file-using-java