Java compression and decompression zip package

Source: Internet
Author: User
Tags create zip create zip file

Online about compression and decompression zip package a large pile, I casually find one. Look, according to their own needs to change a bit, share with you, I hope that the great God:

Package Com.wangpeng.utill;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException ; Import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.util.enumeration;import Java.util.zip.zipentry;import Java.util.zip.zipexception;import Java.util.zip.zipfile;import java.util.zip.zipoutputstream;/** * @author Wangpeng */public class ToolOfZip {/** * Unzip zip package * @param inputpath The path of the unpacked Zip package * @param targetpath the folder extracted to * @param isclear if the folder is clear */public static void UnZip (String i Nputpath,string TargetPath, Boolean isclear) {try {if (null = = TargetPath | | "". Equals (TargetPath)) {TargetPath = inputpath.substring (0,inputpath.lastindexof ("."));} ZipFile zipfile = new ZipFile (new File (InputPath)); enumeration<? Extends zipentry> Entrys = Zipfile.entries (); while (Entrys.hasmoreelements ()) {ZipEntry entry = Entrys.nextelement () ; String name = Entry.getname ();//Aaa\testdir\xxxxx\eee.txtstring strtargetpath=targetpath+ "/" +name; String strtargetdir=strtargetpath.substring (0, Strtargetpath.lastindexof (file.separator)); if (Entry.isDirectory ()) { File dir = new file (Strtargetpath), if (!dir.exists ()) {dir.mkdirs ();}} else {file Dir = new File (Strtargetdir), if (!dir.exists ()) {dir.mkdirs ();} File File = new file (Strtargetpath), if (File.exists () && isclear) {file.delete ();} if (!file.exists ()) {InputStream in = Zipfile.getinputstream (entry); Tooloffile.copyfile (in, File.getabsolutepath ());}}}} catch (Zipexception ex) {ex.printstacktrace ()} catch (IOException ex) {ex.printstacktrace ();} catch (Exception e) {E.pri Ntstacktrace ();}} /** * Create zip file * * @param sourcepath * File or folder path * @param zippath * Generated zip file exists path (contains file name) */public STA tic void Createzip (String sourcepath, String zippath) {FileOutputStream fos = null; Zipoutputstream Zos = null;try {fos = new FileOutputStream (zippath); zos = new Zipoutputstream (FOS); Writezip (new File (sour Cepath), "", Zos);} catch (FileNotFoundException e) {e.printstacktrAce ();} Finally {try {if (Zos! = null) {Zos.close ()}} catch (IOException e) {e.printstacktrace ()}}} private static void Writezip (File inFile, String parentpath,zipoutputstream zipoutstream) {if (infile.exists ()) {if (infi Le.isdirectory ()) {Parentpath + = Infile.getname () + file.separator; file[] files = infile.listfiles (); for (file file:files) {writezip (file, Parentpath, Zipoutstream);}} else {FileInputStream Fileinstream = null;try {fileinstream = new FileInputStream (inFile); ZipEntry entry = new ZipEntry (Parentpath + infile.getname ()); Zipoutstream.putnextentry (entry); byte[] Buff = new byte[ 1024];int len;while (len = fileinstream.read (buff))! =-1) {zipoutstream.write (buff, 0, Len); Zipoutstream.flush ();}} catch (FileNotFoundException e) {e.printstacktrace ()} catch (IOException e) {e.printstacktrace ();} finally {try {if (fil Einstream! = null) {Fileinstream.close ();}} catch (IOException e) {e.printstacktrace ();}}}} public static void Main (string[] args) {String OldPath = "f:/test/ddd/"; String NewPath = "f:/test/ccc.zip";//Toolofzip.createzip (OldPath, NewPath);//toolofzip.unzip (NewPath, NULL, FALSE); System.out.println (); System.out.println ("---------OK----------");}}


Java compression and decompression zip package

Related Article

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.