Java uses Apache's ant compression and decompression file (Zip)

Source: Internet
Author: User

The method in ant can be used to implement compression and decompression of zip, including Chinese.

The specific implementation code is as follows:

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import Java.util.zip.ZipOutputStream;
Import Org.apache.tools.ant.Project;
Import Org.apache.tools.ant.taskdefs.Expand;

Import Org.apache.tools.zip.ZipEntry; /** * * @author Yaohucaizi/public class Ziputil {/** * * * @param file to compress files * @param zipfile compression
        File Storage Place */public static void Zip (file file, file ZipFile) {Zipoutputstream outputstream = null;
            try {outputstream = new Zipoutputstream (new FileOutputStream (ZipFile));
            ZipFile (outputstream, File, "");
                if (OutputStream!= null) {Outputstream.flush ();
            Outputstream.close ();
        } catch (IOException ex) {Logger.getlogger (ZipUtil.class.getName ()). log (Level.severe, NULL, ex);
  Finally {try {              Outputstream.close ();
            The catch (IOException ex) {Logger.getlogger (ZipUtil.class.getName ()). log (Level.severe, NULL, ex); }}/** * * @param output Zipoutputstream Object * @param file or folder to compress * @param BasePath Entry root directory/private static void ZipFile (Zipoutputstream output, file file, String basepath) {Filei
        Nputstream input = null; try {//file is directory if (File.isdirectory ()) {//Get the list of files in the current directory file Lis
                T[] = File.listfiles (); BasePath = BasePath + (basepath.length () = = 0?
                "": "/") + File.getname ();
                Cyclic recursion compresses each file for (file f:list) {ZipFile (output, F, basepath); } else {//compressed file BasePath = (basepath.length () = 0? "": BasePath + "/") + File.getnamE ();
                System.out.println (BasePath);
                Output.putnextentry (New ZipEntry (BasePath));
                input = new FileInputStream (file);
                int readlen = 0;
                byte[] buffer = new byte[1024 * 8];
                while ((Readlen = input.read (buffer, 0, 1024 * 8))!=-1) {output.write (buffer, 0, Readlen);
        A catch (Exception ex) {ex.printstacktrace ());
                Finally {//close stream if (input!= null) {try {input.close (); The catch (IOException ex) {Logger.getlogger (ZipUtil.class.getName ()). log (Level.severe,
                NULL, ex); /** * * * @param sourcezip to extract file path * @param destdir extract to Path * p ublic static void UnZip (String sourcezip, String destdir) {//Guaranteed folder path is Last "/" or "\" char Lastchar = dEstdir.charat (Destdir.length ()-1);
        if (Lastchar!= '/' && Lastchar!= ' \) {destdir = File.separator;
        Project P = new Project ();
        Expand e = new Expand ();
        E.setproject (P);
        E.SETSRC (New File (Sourcezip));
        E.setoverwrite (FALSE);
        E.setdest (New File (Destdir));   
         /* Ant the Zip tool default compression encoding for UTF-8 encoding, while WinRAR software compression is used by Windows default GBK or GB2312 encoding so when decompression to develop coding format
        * * e.setencoding ("GBK");
    E.execute ();
        public static void Main (string[] args) {String SourcePath = "C:/model.zip";
        String destpath = "C:/test";
        UnZip (SourcePath, DestPath);
    Zip (new file ("C:/test/model"), New file ("D:/model.zip"));
 }
}

The above code can realize zip file compression and decompression, this process required jar package Click link download Download link


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.