Apache Ant for a zip decompression operation sample sharing _java

Source: Internet
Author: User
Tags zip

Need to import Ant.jar package, Apache website (http://ant.apache.org/bindownload.cgi) can download.

Copy Code code as follows:

Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.zip.ZipOutputStream;

Import Org.apache.tools.ant.Project;
Import Org.apache.tools.ant.taskdefs.Expand;
Import Org.apache.tools.zip.ZipEntry;

Import Com.xyq.io.util.CloseIoUtil;

public class Ziputil {

private static final String ENCODE = "UTF-8";

 public static void Zip (String inputfilepath, String zipfilename) {

  file inputfile = new File (Inputfilepath);
  if (!inputfile.exists ())
   throw New RuntimeException ("Original file does not exist!!!");
  file basetarzipfile = new File (zipfilename). Getparentfile ();
  if (!basetarzipfile.exists () &&!basetarzipfile.mkdirs ())
   throw New RuntimeException ("Target file cannot be created!!!");
  bufferedoutputstream bos = NULL;
  fileoutputstream out = null;
  zipoutputstream zout = null;
  try {
   //Create a file output object out, hint: note Chinese support
   out = new FileOutputStream (New String (Zipfilename.getbytes (ENCODE));
   bos = new Bufferedoutputstream (out);
   //The file out of the zip output stream
   zout = new Zipoutputstream (BOS);
   zip (Zout, Inputfile, Inputfile.getname ());
   closeioutil.closeall (Zout, Bos, out);

catch (Exception e) {
E.printstacktrace ();
}
}

private static void Zip (Zipoutputstream zout, file file, String base) {

try {
If the file handle is a directory
if (File.isdirectory ()) {
Get the files in the directory
file[] Listfiles = File.listfiles ();
Create a Zip entry
Zout.putnextentry (new ZipEntry (base + "/"));
Base = (Base.length () = = 0? "": Base + "/");
if (listfiles!= null && listfiles.length > 0)
Traverse directory to the file
for (File f:listfiles)
Recursive access to this method
Zip (Zout, F, base + f.getname ());
}
If the file handle is a file
else {
if (base = = "") {
Base = File.getname ();
}
Fill in file handle
Zout.putnextentry (new ZipEntry (base));
Start compression
Read from file inflow, write zip out stream
WriteFile (zout, file);
}

catch (Exception e) {
E.printstacktrace ();
}
}

private static void WriteFile (Zipoutputstream zout, file file)
Throws IOException {

FileInputStream in = null;
Bufferedinputstream bis = null;
in = new FileInputStream (file);
bis = new Bufferedinputstream (in);
int len = 0;
byte[] buff = new byte[2048];
while (len = bis.read (Buff))!=-1)
Zout.write (buff, 0, Len);
Zout.flush ();
Closeioutil.closeall (bis, in);
}

/****
* Decompression
*
* @param Zippath
* Zip file path
* @param DestinationPath
* The location of the decompression destination
* @param ecode
* Coded character set for file name
*/
public static void UnZip (String zippath, String destinationpath) {

File ZipFile = new file (Zippath);
if (!zipfile.exists ())
throw new RuntimeException ("Zip file" + Zippath
+ "does not exist.");

Project proj = new Project ();
Expand Expand = new Expand ();
Expand.setproject (proj);
Expand.settasktype ("unzip");
Expand.settaskname ("unzip");
EXPAND.SETSRC (ZipFile);
Expand.setdest (New File (DestinationPath));
Expand.setencoding (ENCODE);
Expand.execute ();
System.out.println ("Unzip done!!!");
}

public static void Main (string[] args) {

String dir = new String ("f:\\ my backup \ Document \\myeclipse+9.0 official version cracked and activated (pro-Test available)");
dir = new String ("f:/111.jpg");
Zip (dir, "F:/bzbxb/zipant.zip");
UnZip ("F:/bzbxb/zipant.zip", "f:/xx/xx/");
}
}

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.