欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Jar包的压缩和解压缩,Jar包压缩解压缩,java压缩和解压缩ja

来源: javaer 分享于  点击 4429 次 点评:178

Jar包的压缩和解压缩,Jar包压缩解压缩,java压缩和解压缩ja


java压缩和解压缩jar包

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package externalatool;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import java.util.jar.JarEntry;import java.util.jar.JarOutputStream;/** * jar压缩类 * @author Administrator */public class JARCompress { /**  * jar压缩功能测试.  * @param dir  *            所要压缩的目录名(包含绝对路径)  * @param jarName  *            压缩后的文件名  * @throws Exception  */ public void doIt(String dir, String jarName) throws Exception {  File folderObject = new File(dir);  if (folderObject.exists()) {   List fileList = getSubFiles(new File(dir));   // 压缩文件名   FileOutputStream fos = new FileOutputStream(jarName);   JarOutputStream zos = new JarOutputStream(fos);   JarEntry ze = null;   byte[] buf = new byte[1024];   int readLen = 0;   for (int i = 0; i < fileList.size(); i++) {    File f = (File) fileList.get(i);    System.out.println("添加: " + f.getPath() + f.getName());    ze = new JarEntry(getAbsFileName(dir, f));    ze.setSize(f.length());    ze.setTime(f.lastModified());    zos.putNextEntry(ze);    InputStream is = new BufferedInputStream(new FileInputStream(f));    while ((readLen = is.read(buf, 0, 1024)) != -1) {     zos.write(buf, 0, readLen);    }    is.close();    System.out.println("完成...");   }   zos.close();  } else {   throw new Exception("文件夹不存在!");  } } /**  * 取得指定目录以及其各级子目录下的所有文件的列表,注意,返回的列表中不含目录.  * @param baseDir  *            File 指定的目录  * @return 包含java.io.File的List  */ private List getSubFiles(File baseDir) {  List fileList = new ArrayList();  File[] tmp = baseDir.listFiles();  for (int i = 0; i < tmp.length; i++) {   if (tmp[i].isFile()) {    fileList.add(tmp[i]);   }   if (tmp[i].isDirectory()) {    fileList.addAll(getSubFiles(tmp[i]));   }  }  return fileList; } /**  * 给定根目录及文件的实际路径,返回带有相对路径的文件名,用于zip文件中的路径.  * 如将绝对路径,baseDir\dir1\dir2\file.txt改成 dir1/dir2/file.txt  * @param baseDir  *            java.lang.String 根目录  * @param realFileName  *            java.io.File 实际的文件名  * @return 相对文件名  */ private String getAbsFileName(String baseDir, File realFileName) {  File real = realFileName;  File base = new File(baseDir);  String ret = real.getName();  while (true) {   real = real.getParentFile();   if (real == null)    break;   if (real.equals(base))    break;   else {    ret = real.getName() + "/" + ret;   }  }  return ret; }// public static void main(String args[]) {//  System.out.println("开始压缩...");//  try {//   new JARCompress().doIt(//     "E:\\Eclipse\\JarCompressAndDecompress\\et20",//     "e:\\out.jar");//  } catch (Exception e1) {//   // TODO Auto-generated catch block//   e1.printStackTrace();//  }//  System.out.println("----------压缩完成!----------");// }}解压缩方法类:/* * To change this template, choose Tools | Templates * and open the template in the editor. */package externalatool;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.jar.JarEntry;import java.util.jar.JarFile;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * jar 解压缩类 * @author Administrator */public class JARDecompress { public void doIt(String jarFileName , String outputPath) {  System.out.println("开始解压文件......");  try {   // 执行解压   decompress(jarFileName, outputPath);  } catch (IOException e) {   e.printStackTrace();   System.out.println("提取文件失败!");//   dealError(outputPath);   System.out.println("安装失败!");   return;  } } /**  * 解压缩JAR包  *  * @param fileName  *            文件名  * @param outputPath  *            解压输出路径  * @throws IOException  *             IO异常  */ private void decompress(String fileName, String outputPath)   throws IOException {  //创建解压包路径  if (!outputPath.endsWith(File.separator)) {   outputPath += File.separator;  }  //创建jar文件对象引用  JarFile jf = new JarFile(fileName);  //枚举jar文件中的每个元素  for (Enumeration e = jf.entries(); e.hasMoreElements();) {   JarEntry je = (JarEntry) e.nextElement(); //jar中元素条目   String outFileName = outputPath + je.getName(); //解压文件的名称   File f = new File(outFileName); //创建解压文件对象   System.out.println(f.getAbsolutePath()); //打印绝对路径   // 创建该路径的目录和所有父目录   createFatherDir(outFileName);   // 如果是目录,则直接进入下一个循环   if (f.isDirectory()) {    continue;   }   InputStream in = null;   OutputStream out = null;   try {    in = jf.getInputStream(je);    FileOutputStream fos = new FileOutputStream(f);    out = new BufferedOutputStream(fos);    byte[] buffer = new byte[2048];    int nBytes = 0;    while ((nBytes = in.read(buffer)) > 0) {     out.write(buffer, 0, nBytes);    }   } catch (IOException ioe) {    throw ioe;   } finally {    try {     if (out != null) {      out.flush(); //立刻写      out.close();     }    } catch (IOException ioe) {     throw ioe;    } finally {     if (in != null) {      in.close();     }    }   }  } } /**  * 循环创建父目录,正则表达式方式  * @param outFileName  */ private void createFatherDir(String outFileName) {  // 匹配分隔符  Pattern p = Pattern.compile("[/\\" + File.separator + "]");  Matcher m = p.matcher(outFileName);  // 每找到一个匹配的分隔符,则创建一个该分隔符以前的目录  while (m.find()) {   int index = m.start();   String subDir = outFileName.substring(0, index);   File subDirFile = new File(subDir);   if (!subDirFile.exists())    subDirFile.mkdir();  } }// public static void main(String[] args){//                String jarFileName = "e:\\out.jar";//  String outputPath = "e:\\out";//  new JARDecompress().doIt(jarFileName , outputPath);//  System.out.println("----------解压完成!----------");// }}
相关栏目:

用户点评