利用ant实现解压缩
package com.ecarvalues.service.test; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.lang.StringUtils; import org.apache.tools.zip.ZipOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; /** * @author jeff */ public class ZipUtil { public static boolean doZip(String filesDirPath, String zipFilePath) { return doZip(new File(filesDirPath), zipFilePath); } private static boolean doZip(File inputFile, String zipFileName) { ZipOutputStream out = null; try { out = new ZipOutputStream(new FileOutputStream(zipFileName)); boolean result = doZip(out, inputFile, ""); return result; } catch (FileNotFoundException ex) { //ex.printStackTrace(); return false; } catch (IOException ex) { //ex.printStackTrace(); return false; } finally { try { out.close(); } catch (IOException ex) { //ex.printStackTrace(); return false; } } } private static boolean doZip(ZipOutputStream out, File f, String base) { try { if (f.isDirectory()) { File[] fl = f.listFiles(); out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { doZip(out, fl[i], base + fl[i].getName()); } } else { out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); } return true; } catch (IOException ex) { //ex.printStackTrace(); return false; } } public static boolean unZip(String srcFile, String dest, boolean deleteFile) { try { if(StringUtils.isNotBlank(dest)){ if(!dest.endsWith("/")) dest=dest.trim()+"/"; }else{ } File file = new File(srcFile); if (!file.exists()) { //throw new RuntimeException("解压文件不存在!"); return false; } ZipFile zipFile = new ZipFile(file); Enumeration e = zipFile.getEntries(); while (e.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) e.nextElement(); if (zipEntry.isDirectory()) { String name = zipEntry.getName(); name = name.substring(0, name.length() - 1); File f = new File(dest + name); f.mkdirs(); } else { File f = new File(dest + zipEntry.getName()); f.getParentFile().mkdirs(); f.createNewFile(); InputStream is = zipFile.getInputStream(zipEntry); FileOutputStream fos = new FileOutputStream(f); int length = 0; byte[] b = new byte[1024]; while ((length = is.read(b, 0, 1024)) != -1) { fos.write(b, 0, length); } is.close(); fos.close(); } } if (zipFile != null) { zipFile.close(); } if (deleteFile) { file.deleteOnExit(); } return true; } catch (IOException ex) { return false; } } public static void main(String[] args) throws Exception { //压缩文件夹 // boolean resultOfZip = ZipUtil.doZip("/Users/jeff/study/data/test", "/Users/jeff/study/data/test_chrysler.zip"); //解压缩 boolean resultOfUnZip = ZipUtil.unZip("/Users/jeff/study/data/MVGFiles.zip", "/Users/jeff/study/data/chrysler/", false); System.out.println("压缩结果:" + resultOfUnZip + "\n解压缩结果:" ); } }
程序实现了ZIP压缩。共分为2部分 : 压缩(compression)与解压(decompression)
大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压。 需在代码中自定义源输入路径和目标输出路径。
package com.han; import java.io.*; import java.util.zip.*; /** * 程序实现了ZIP压缩。共分为2部分 : * 压缩(compression)与解压(decompression) * <p> * 大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压。 * 需在代码中自定义源输入路径和目标输出路径。 * <p> * 在本段代码中,实现的是压缩部分;解压部分见本包中decompression部分。 * @author HAN * */ public class CopyOfMyZipCompressing { private int k=1; //定义递归次数变量 public CopyOfMyZipCompressing() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub long startTime=System.currentTimeMillis(); CopyOfMyZipCompressing book=new CopyOfMyZipCompressing(); try { book.zip("C:\\Users\\HAN\\Desktop\\stock\\SpectreCompressed.zip", //自定义的zip输出路径 new File("C:\\Users\\HAN\\Desktop\\CombinedSpectres.txt")); //自定义的源输入路径,即要压缩的文件或文件夹 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } long endTime=System.currentTimeMillis(); System.out.println("耗费时间: "+(endTime-startTime)+" ms"); } private void zip(String zipFileName, File inputFile) throws Exception{ System.out.println("压缩中..."); ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName)); BufferedOutputStream bo=new BufferedOutputStream(out); zip(out,inputFile, "/"+inputFile.getName(),bo); bo.close(); out.close(); //输出流关闭 System.out.println("压缩完成"); } private void zip(ZipOutputStream out, File f, String base, BufferedOutputStream bo) throws Exception{ //方法重载 if (f.isDirectory()){ File[] fl=f.listFiles(); for(int i=0;i<fl.length;i++){ zip(out, fl[i],base+"/"+fl[i].getName(),bo); //递归遍历子文件夹 } System.out.println("第"+k+"次递归"); k++; }else{ out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入点base System.out.println(base); FileInputStream in=new FileInputStream(f); BufferedInputStream bi=new BufferedInputStream(in); int b; while((b=bi.read())!=-1){ bo.write(b); //将字节流写入当前zip目录 } bi.close(); in.close(); //输入流关闭 } } } package com.han; import java.io.*; import java.util.zip.*; /** * 程序实现了ZIP压缩。共分为2部分 : * 压缩(compression)与解压(decompression) * <p> * 大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压。 * 需在代码中自定义源输入路径和目标输出路径。 * <p> * 在本段代码中,实现的是解压部分;压缩部分见本包中compression部分。 * @author HAN * */ public class CopyOfMyzipDecompressing { public static void main(String[] args) { // TODO Auto-generated method stub long startTime=System.currentTimeMillis(); try { ZipInputStream Zin=new ZipInputStream(new FileInputStream( "C:\\Users\\HAN\\Desktop\\stock\\SpectreCompressed.zip"));//输入源zip路径 BufferedInputStream Bin=new BufferedInputStream(Zin); String Parent="C:\\Users\\HAN\\Desktop"; //输出路径(文件夹目录) File Fout=null; ZipEntry entry; try { while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){ Fout=new File(Parent,entry.getName()); if(!Fout.exists()){ (new File(Fout.getParent())).mkdirs(); } FileOutputStream out=new FileOutputStream(Fout); BufferedOutputStream Bout=new BufferedOutputStream(out); int b; while((b=Bin.read())!=-1){ Bout.write(b); } Bout.close(); out.close(); System.out.println(Fout+"解压成功"); } Bin.close(); Zin.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } long endTime=System.currentTimeMillis(); System.out.println("耗费时间: "+(endTime-startTime)+" ms"); } }