Compression Tool Class-Ziputils.java

Source: Internet
Author: User

The compression tool class, provides the method which compresses the file, extracts the file.

The source code is as follows: (Click to download-Ziputils.java, Folderutils.java, Ant-1.7.0.jar, Commons-io-2.4.jar, Commons-lang-2.6.jar)

ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Enumeration;Importorg.apache.commons.io.FilenameUtils;Importorg.apache.commons.io.IOUtils;Importorg.apache.commons.lang.ArrayUtils;Importorg.apache.commons.lang.StringUtils;ImportOrg.apache.tools.zip.ZipEntry;ImportOrg.apache.tools.zip.ZipFile;ImportOrg.apache.tools.zip.ZipOutputStream;/*** Compression Tool class **/ Public classZiputils {Private Static FinalString default_charset = "UTF-8"; /*** Compressed Folder * *@paramZipfilename * Name of the file after packaging, including path *@paramSourcefolder * The path of the folder or file that needs to be packaged *@paramZippathname * Package destination folder name, empty means package directly to root*/     Public Static voidZip (string zipfilename, String Sourcefolder, String zippathname)throwsException {Zipoutputstream out=NULL; Try{File ZipFile=NewFile (zipfilename);            Folderutils.mkdirs (Zipfile.getparent ()); out=NewZipoutputstream (ZipFile);            Out.setencoding (Default_charset); if(Stringutils.isnotblank (zippathname)) {Zippathname= Filenameutils.normalizenoendseparator (Zippathname,true) + "/"; } Else{zippathname= "";        } zip (out, sourcefolder, zippathname); } Catch(IOException e) {e.printstacktrace (); Throw NewException (e); } finally{ioutils.closequietly (out); }    }    /*** Compressed Folder * *@paramZipFile * A {@linkjava.lang.String} object. * @paramSource * A {@linkjava.lang.String} object. */     Public Static voidZip (string zipfile, string source)throwsException {File file=NewFile (source); Zip (ZipFile, source, File.isfile ()?StringUtils.EMPTY:file.getName ()); }    /*** Compressed Folder * *@paramZipFile * A {@linkJava.io.File} object. * @paramSource * A {@linkJava.io.File} object. */     Public Static voidZip (file zipfile, file source)throwsException {zip (Zipfile.getabsolutepath (), Source.getabsolutepath ()); }    Private Static voidZip (zipoutputstream zos, string file, String pathName)throwsIOException {File file2zip=Newfile (file); if(File2zip.isfile ()) {Zos.putnextentry (NewZipEntry (PathName +file2zip.getname ())); Ioutils.copy (NewFileInputStream (File2zip.getabsolutepath ()), Zos);            Zos.flush ();        Zos.closeentry (); } Else{file[] files=File2zip.listfiles (); if(Arrayutils.isnotempty (Files)) { for(File f:files) {if(F.isdirectory ()) {Zip (Zos, Filenameutils.normalizenoendseparator (), F.getabsolutepath (),true), Filenameutils.normalizenoendseparator (PathName+ F.getname (),true) + "/"); } Else{zos.putnextentry (NewZipEntry (PathName +f.getname ())); Ioutils.copy (NewFileInputStream (F.getabsolutepath ()), Zos);                        Zos.flush ();                    Zos.closeentry (); }                }            }        }    }    /*** Unzip * *@paramfromzipfile * Zip file path *@paramUnzippath * Decompression Path*/@SuppressWarnings ("Unchecked")     Public Static Final voidUnzip (string fromzipfile, String unzippath)throwsException {fileoutputstream fos=NULL; InputStream is=NULL; String path1=Stringutils.empty; String TempPath=Stringutils.empty; if(!NewFile (Unzippath). Exists ()) {            NewFile (Unzippath). mkdir (); } ZipFile ZipFile=NULL; Try{ZipFile=NewZipFile (Fromzipfile, Default_charset); } Catch(IOException E1) {e1.printstacktrace (); Throw NewException (E1); } File Temp=NewFile (Unzippath); String strpath=Temp.getabsolutepath (); Enumeration<ZipEntry> ENU =zipfile.getentries (); ZipEntry ZipEntry=NULL;  while(Enu.hasmoreelements ()) {ZipEntry=(ZipEntry) enu.nextelement (); Path1=Zipentry.getname (); if(Zipentry.isdirectory ()) {TempPath= Filenameutils.normalizenoendseparator (strpath + file.separator + path1,true); File dir=NewFile (TempPath);                Dir.mkdirs (); Continue; } Else{Bufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; Try{ is=Zipfile.getinputstream (ZipEntry); Bis=NewBufferedinputstream (IS); Path1=Zipentry.getname (); TempPath= Filenameutils.normalizenoendseparator (strpath + file.separator + path1,true); Folderutils.mkdirs (NewFile (TempPath). GetParent ()); FOS=NewFileOutputStream (TempPath); Bos=NewBufferedoutputstream (FOS);                Ioutils.copy (bis, BOS); } Catch(IOException e) {e.printstacktrace (); Throw NewException (e); } finally{ioutils.closequietly (bis);                    ioutils.closequietly (BOS);                    Ioutils.closequietly (IS);                ioutils.closequietly (FOS); }            }        }    }}

Compression Tool Class-Ziputils.java

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.