Apache ant shares the sample zip unzip operation

  • 2020-04-01 02:53:30
  • OfStack

Need to import ant jar package, download apache web site (http://ant.apache.org/bindownload.cgi).


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(" The original file does not exist !!!");
  File basetarZipFile = new File(zipFileName).getParentFile();
  if (!basetarZipFile.exists() && !basetarZipFile.mkdirs())
   throw new RuntimeException(" The target file could not be created !!!");
  BufferedOutputStream bos = null;
  FileOutputStream out = null;
  ZipOutputStream zOut = null;
  try {
   //Create file output object out, note: Chinese support
   out = new FileOutputStream(new String(zipFileName.getBytes(ENCODE)));
   bos = new BufferedOutputStream(out);
   //The ZIP output stream from the ZIP file is connected
   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()) {
    //Gets the files in the directory
    File[] listFiles = file.listFiles();
    //Create ZIP entry
    zOut.putNextEntry(new ZipEntry(base + "/"));
    base = (base.length() == 0 ? "" : base + "/");
    if (listFiles != null && listFiles.length > 0)
     //Traverse the files in the directory
     for (File f : listFiles)
      //Recursively enters this method
      zip(zOut, f, base + f.getName());
   }
   //If the file handle is a file
   else {
    if (base == "") {
     base = file.getName();
    }
    //Fill in the file handle
    zOut.putNextEntry(new ZipEntry(base));
    //To compress
    //Stream read from the file and write ZIP stream out
    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);
 }
 
 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 \ The document \MyEclipse+9.0 Official version cracking and activation ( Close test available )");
  dir = new String("F:/111.JPG");
  zip(dir, "f:/BZBXB/zipant.zip");
  unZip("f:/BZBXB/zipant.zip", "f:/XX/xx/");
 }
}


Related articles: