Java implements tar GZ packaging for multiple files

Source: Internet
Author: User
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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.