This uses Apache Commons compress to extract the. tar.z format files.
For a file Test.tar.z, we can understand the decompression process as:
- Unzip the test.tar.z into Test.tar;
- Unzip the Test.tar to test.
According to an example of Apache Commons compress:
Extract. Z File Example:
inputstream fin = Files. (Paths. ( "Archive.tar.z" )); Bufferedinputstream in = new bufferedinputstream (Fin), outputstream out = Files. newoutputstream (Paths. get ( "Archive.tar" )); Zcompressorinputstream zIn = new zcompressorinputstream (in); final byte [] buffer = new byte [buffersize]; int n = 0 ; while (-1 ! = (n = Zin. (buffer)) {Out. (buffer, 0 , n);} Out. close (); ZIn. close ();
Example of extracting a tar file:
Adding an entry to a tar archive:
newTarArchiveEntry(name);entry.setSize(size);tarOutput.putArchiveEntry(entry);tarOutput.write(contentOfEntry);tarOutput.closeArchiveEntry();
Reading entries from a tar archive:
TarArchiveEntry entry = tarInput.getNextTarEntry();bytenewbyte[entry.getSize()];LOOP UNTIL entry.getSize() HAS BEEN READ { tarInput.read(content, offset, content.length - offset);}
The MAVEN address for Apache Commons compress is as follows:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --><dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.16.1</version></dependency>
Test procedure:
Package test;import Java.io.BufferedInputStream;import Java.io.File;import Java.io.FileInputStream;import Java.io.FileOutputStream;import java.io.IOException;import Java.io.InputStream;import Java.io.OutputStream;import java.nio.file.Files;import java.nio.file.Paths;import java.util.ArrayList;import java.util.List;import org.apache.commons.compress.archivers.tar.TarArchiveEntry;import Org.apache.commons.compress.archivers.tar.TarArchiveInputStream;import Org.apache.commons.compress.compressors.z.zcompressorinputstream;import org.apache.commons.compress.utils.IOUtils; Public classTestuncompresstarz { Public Static void compresstest()throwsIOException {//Will tar. Z Unzip to TarSystem. out.println("Uncompress tar. Z to tar ... "); InputStream fin = Files.Newinputstream(Paths.Get("D:\\Test.tar.z ")); Bufferedinputstream in =NewBufferedinputstream (Fin); OutputStream out = Files.Newoutputstream(Paths.Get("D:\\Test.tar ")); Zcompressorinputstream zIn =New Zcompressorinputstream(in);byte[] buffer =New byte[1024x768];intn =0; while(-1! = (n = zIn.Read(buffer))) {out.Write(Buffer,0, n); } out.Close(); ZIn.Close(); System. out.println("Uncompress tar. Z to tar finished! ");//Unzip tar into a folderSystem. out.println("Uncompress tar to directory file ..."); List<string> FileNames =NewArraylist<string> (); InputStream InputStream =NewFileInputStream (NewFile ("D:\\Test.tar ")); Tararchiveinputstream Tarin =New Tararchiveinputstream(InputStream,1024x768); Tararchiveentry entry =NULL; while(Entry = Tarin.Getnexttarentry()) !=NULL) {System. out.println("... "+ entry.GetName() +" , "+ entry.isdirectory()); FileNames.Add(Entry.GetName());if(Entry.isdirectory()) {//Is directoryFile TmpDir =NewFile ("D:\\Test\\"+ entry.GetName());if(TmpDir.exists() ==false) {TmpDir.mkdirs(); } }Else{//is a fileFile tmpfile =NewFile ("D:\\Test\\"+ entry.GetName()); File TmpDir = tmpfile.Getparentfile(); System. out.println("Parent:"+ TmpDir.GetAbsolutePath());if(TmpDir.exists() ==false) {TmpDir.mkdirs(); } OutputStream OutputStream =NewFileOutputStream (tmpfile);intLength =0;byte[] B =New byte[1024x768]; while(length = Tarin.Read(b))! =-1) {OutputStream.Write(b,0, length); }}} ioutils.closequietly(Tarin); System. out.println("Uncompress tar to directory file finished!"); System. out.println("All finished!"); } Public Static void Main(string[] args)throwsIOException {compresstest(); }}
In this test program, first unzip the test.tar.z to Test.tar and then unzip the Test.tar into a file named Test.
Java extract tar.z file (using Apache commons-compress)