java对jar的解压和压缩

由于2BizBox ERP全部代码重构,调整了些目录结构,所以也需要对相应的jar包进行修改,这里用到java对jar的解压和压缩的技术,第一次做到这个,也遇到些情况,在这里跟大家分享一下。
java对jar的解压和压缩,主要用到了JarFile和JarEntry两个类。
JarFile用于读取jar文件的内容;JarEntry用于表示jar文件条目。


条目指的是什么?


给举个列子,假设一个压缩文件里面的路径是com/apple/iphone/,在iphone下面有两个文件分别为iphone4S.pdf和iphone5.pdf。
那么这个压缩文件的条目就是:
com/
com/apple/
com/apple/iphone/
com/apple/iphone/iphone4S.pdf
com/apple/iphone/iphone5.pdf
包括文件的条目和文件夹的条目,所以要注意,在压缩的时候也要把文件夹的条目给压缩进去。
我在压缩的时候,漏掉了文件夹的条目,结果压缩工具可以正常打开查看文件内容,文件内容也正确,压缩工具也可以解压缩,再用程序读取也可以读取内容,但是jboss一直加载不了这个jar文件。
好了说了那么多,主要是让大家注意压缩的时候不要漏掉文件夹的条目,下面上代码,代码是将以上的目录结构由com/apple/iphone/改为apple/iphone/

package update;

import java.io.*;
import java.util.*;
import java.util.jar.*;
import java.util.regex.*;
import java.util.zip.*;


public class ChangeJarFile {
	
	private static void backupJarFile(String jarPath) throws IOException{
		dealJarFile(jarPath+"/ihpones.jar", jarPath+"/Oldiphones/ihpones.jar");
	}
	
	public static void recoverJarFile(String jarPath) throws IOException{
		dealJarFile(jarPath+"/Oldiphones/ihpones.jar", jarPath+"/ihpones.jar");
		deleteFile(jarPath+"/Oldiphones");
	}
	
	private static void dealJarFile(String filePath, String outputPath) throws IOException{
		File jarFile = new File(filePath);
		if(jarFile.exists() && jarFile!=null){
			makeSupDir(outputPath);
			writeFile(jarFile, new File(outputPath));
		}
	}
	
	private static void makeSupDir(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();
		}
	} 
	
	private static void zipFile(String jarPath) throws IOException{
		JarOutputStream ops = new JarOutputStream(new FileOutputStream(jarPath+"/ihpones.jar"));
		File inFile = new File(jarPath+"/iphones");
		zipFileEntry("", inFile, ops);
		ops.close();
	}
	
	private static void zipFileEntry(String base, File inFile, JarOutputStream ops) throws IOException{
		if(inFile.isDirectory()){
			File[] files = inFile.listFiles();
			ops.putNextEntry(new ZipEntry(base + "/"));
			base = base.length() == 0 ? "" : base + "/";
			for(File file:files){
				zipFileEntry(base+file.getName(), file, ops);
			}
		}else{
			ops.putNextEntry(new JarEntry(base));
			InputStream ips = new FileInputStream(inFile);
			int len = 0;
			byte[] buffer = new byte[1024]; 
			while((len = ips.read(buffer)) != -1){
				ops.write(buffer,0,len);
				ops.flush(); 
			}
			ips.close();
		}
	}
	
	private static void unZipFile(String jarPath) throws IOException{
		JarFile jarFile = new JarFile(jarPath+"/ihpones.jar");
		Enumeration<JarEntry> jarEntrys = jarFile.entries();
		while(jarEntrys.hasMoreElements()){
			JarEntry jarEntry = jarEntrys.nextElement();
			jarEntry.getName();
			String outFileName = jarPath +"/ihpones/"+ jarEntry.getName(); 
			if(outFileName.contains("com/") ){
				outFileName = outFileName.replace("com/", "");
			}
			File f = new File(outFileName); 
			makeSupDir(outFileName); 
			if(jarEntry.isDirectory()){
				continue;
			}
			writeFile(jarFile.getInputStream(jarEntry), f);
		}
	}
	
	private static void deleteFile(String jarPath) throws IOException{
		File delFile = delFile = new File(jarPath);
		if(delFile.exists() && delFile.isDirectory()){
			if(delFile.listFiles().length==0){
				delFile.delete();
			} else {
				for(File file:delFile.listFiles()){
					if(file.isDirectory()){
						deleteFile(file.getAbsolutePath());
					}
					file.delete();
				}
			}
		}
		if(delFile.exists() && delFile.isDirectory() && delFile.listFiles().length==0){
			delFile.delete();
		} 
	}
	
	private static void writeFile(File inputFile, File outputFile) throws IOException{
		writeFile(new FileInputStream(inputFile), outputFile);
	}
	
	private static void writeFile(InputStream ips, File outputFile) throws IOException{
		OutputStream ops = new BufferedOutputStream(new FileOutputStream(outputFile));
		try{ 
			byte[] buffer = new byte[1024]; 
			int nBytes = 0; 
			while ((nBytes = ips.read(buffer)) > 0){ 
				ops.write(buffer, 0, nBytes); 
			} 
		}catch (IOException ioe){ 
			throw ioe; 
		} finally { 
			try { 
				if (null != ops){ 
					ops.flush(); 
					ops.close(); 
				} 
			} catch (IOException ioe){ 
				throw ioe; 
			} finally{ 
				if (null != ips){ 
					ips.close(); 
				} 
			} 
		}
	}
	
	public static void changeJarFile(String jarPath){
		try {
			backupJarFile(jarPath);
			unZipFile(jarPath);
			zipFile(jarPath);
			deleteFile(jarPath+"/iphones");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值