Package test; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import java.util.zip. gzipoutputstream; import org.apache.commons.compress.archivers.tar. tararchiveentry; import org.apache.commons.compress.archivers.tar. tararchiveoutputstream; import Org. apache. commons. compress. utils. ioutils; public class Daba O_tar_gz {/***** @ title: Pack * @ description: pack a group of files into a tar package * @ Param sources * the array of the original files to be packaged * @ Param target * the packaged file * @ return file returns the packaged file * @ throws * /Public file pack (file [] sources, file target) {fileoutputstream out = NULL; try {out = new fileoutputstream (target);} catch (filenotfoundexception E1) {e1.printstacktrace ();} tararchiveoutputstream OS = new tararchiveoutputstream (out ); for (File file: S Ources) {try {OS. putarchiveentry (New tararchiveentry (File); ioutils. copy (New fileinputstream (file), OS); OS. closearchiveentry ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} if (OS! = NULL) {try {OS. flush (); OS. close (); system. out. println ("package file:" + target);} catch (ioexception e) {e. printstacktrace () ;}} return target;}/***** @ title: compress * @ description: use gzip to compress the file * @ Param source * the file to be compressed * @ return file returns the compressed file * @ throws */Public File compress (File Source) {file target = new file (source. getname () + ". GZ "); fileinputstream in = NULL; gzipoutputstream out = NULL; try {In = ne W fileinputstream (source); out = new gzipoutputstream (New fileoutputstream (target); byte [] array = new byte [1024]; int number =-1; while (number = in. read (array, 0, array. length ))! =-1) {out. write (array, 0, number) ;}} catch (filenotfoundexception e) {e. printstacktrace (); return NULL;} catch (ioexception e) {e. printstacktrace (); return NULL;} finally {If (in! = NULL) {try {in. Close () ;}catch (ioexception e) {e. printstacktrace (); return NULL ;}} if (OUT! = NULL) {try {out. close (); system. out. println ("package file:" + target);} catch (ioexception e) {e. printstacktrace (); return NULL ;}} return target ;}}