将文件夹压缩为jar包——JAVA小工具
2012-04-10 10:41
531 查看
工具下载 经常遇到修改Jar包里的内容,先前的方法是先解压,然后压缩为zip,然后将后缀名改为jar。
本java小工具是可实现批量压缩文件包为jar包,核心代码如下:
/**
* zip压缩
* @param parentDirPath 要压缩文件夹的父文件夹
* @param targetPath 目标文件夹
*/
private static void zipDirectory(String parentDirPath,String targetPath)
{
try {
File dirFile=new File(parentDirPath);
File[] listArr = dirFile.listFiles();
for (File childFile : listArr) {
//File childFile=new File(child);
if(childFile.isDirectory())
{
if(list.size()>0)
list.clear();
byte b[] = new byte[128];
//压缩文件的保存路径
String zipFile =targetPath+File.separator+childFile.getName()+".jar";
//压缩文件目录
String filepath =childFile.getAbsolutePath()+File.separator;
List fileList = allFile(filepath);
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
//使用输出流检查
CheckedOutputStream cs = new CheckedOutputStream(fileOutputStream, new CRC32());
//声明输出zip流
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
cs));
for (int i = 0; i < fileList.size(); i++) {
InputStream in = new FileInputStream((String)fileList.get(i));
String fileName = ((String)(fileList.get(i))).replace(File.separatorChar,'/');
System.out.println("ziping " + fileName);
String tmp= childFile.getName()+"/";
fileName = fileName.substring(fileName.lastIndexOf(tmp)+childFile.getName().length()+1);
ZipEntry e = new ZipEntry(fileName);
out.putNextEntry(e);
int len = 0;
while((len = in.read(b)) != -1) {
out.write(b,0,len);
}
out.closeEntry();
}
out.close();
System.out.println("done!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static List allFile(String path)
{
File file = new File(path);
String[] array = null;
String sTemp = "";
if(file.isDirectory())
{
}else{
return null;
}
array= file.list();
if(array.length>0)
{
for(int i=0;i<array.length;i++)
{
sTemp = path+array[i];
file = new File(sTemp);
if(file.isDirectory())
{
allFile(sTemp+"/");
}else{
list.add(sTemp);
}
}
}else{
return null;
}
return list;
}
工具使用方法: 下载
第一个参数是要压缩文件夹的上一级路径;第二个参数为目标文件夹路径
java 源代码工程:源代码下载
本博客声明:本人的技术探索过程中,得到了国信司南公司领导方面支持。今后,本人博客里的所有技术探索成果将归“无痕客”、“国信司南”和“博客园”三方共同所有,原创作品如需转载,请注明本博客声明。
本java小工具是可实现批量压缩文件包为jar包,核心代码如下:
/**
* zip压缩
* @param parentDirPath 要压缩文件夹的父文件夹
* @param targetPath 目标文件夹
*/
private static void zipDirectory(String parentDirPath,String targetPath)
{
try {
File dirFile=new File(parentDirPath);
File[] listArr = dirFile.listFiles();
for (File childFile : listArr) {
//File childFile=new File(child);
if(childFile.isDirectory())
{
if(list.size()>0)
list.clear();
byte b[] = new byte[128];
//压缩文件的保存路径
String zipFile =targetPath+File.separator+childFile.getName()+".jar";
//压缩文件目录
String filepath =childFile.getAbsolutePath()+File.separator;
List fileList = allFile(filepath);
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
//使用输出流检查
CheckedOutputStream cs = new CheckedOutputStream(fileOutputStream, new CRC32());
//声明输出zip流
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
cs));
for (int i = 0; i < fileList.size(); i++) {
InputStream in = new FileInputStream((String)fileList.get(i));
String fileName = ((String)(fileList.get(i))).replace(File.separatorChar,'/');
System.out.println("ziping " + fileName);
String tmp= childFile.getName()+"/";
fileName = fileName.substring(fileName.lastIndexOf(tmp)+childFile.getName().length()+1);
ZipEntry e = new ZipEntry(fileName);
out.putNextEntry(e);
int len = 0;
while((len = in.read(b)) != -1) {
out.write(b,0,len);
}
out.closeEntry();
}
out.close();
System.out.println("done!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static List allFile(String path)
{
File file = new File(path);
String[] array = null;
String sTemp = "";
if(file.isDirectory())
{
}else{
return null;
}
array= file.list();
if(array.length>0)
{
for(int i=0;i<array.length;i++)
{
sTemp = path+array[i];
file = new File(sTemp);
if(file.isDirectory())
{
allFile(sTemp+"/");
}else{
list.add(sTemp);
}
}
}else{
return null;
}
return list;
}
工具使用方法: 下载
第一个参数是要压缩文件夹的上一级路径;第二个参数为目标文件夹路径
java 源代码工程:源代码下载
本博客声明:本人的技术探索过程中,得到了国信司南公司领导方面支持。今后,本人博客里的所有技术探索成果将归“无痕客”、“国信司南”和“博客园”三方共同所有,原创作品如需转载,请注明本博客声明。
相关文章推荐
- java中通过JDK工具手动压缩JAR包时package路径必须严格对应,否则可能压缩JAR包成功,但执行不了
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- java——压缩文件或者文件夹
- Java中的jar包全部都是压缩文件
- JAWR【一个java项目的javascript和CSS集成和压缩工具】
- Java代码压缩文件夹及多文件压缩---简单案例
- 推荐一款带入java依赖包的jar包工具fatJar
- java把指定文件夹下的所有文件压缩成zip包
- jxl.jar 通过java操作excel表格的工具类库
- java中jar命令打包一个文件夹下的所有文件
- 使用java对文件或文件夹进行压缩和加密
- java project中把jar的类库放到那个文件夹的?
- JAVA实现压缩文件或文件夹
- java开发中常见的各种插件、工具、jar版本的查看方法
- Java 压缩指定文件夹
- java解压缩zip文件,java创建zip文件,java压缩文件,java解压文件,用到ant.jar解决汉字乱码
- Java工具之javac---javadoc--javah--jar
- 用java压缩文件夹/文件
- IO_字节流_节点流_文件夹拷贝_工具制作_文件后缀与软件的关系JAVA149、163
- 使用java对文件或文件夹进行压缩和加密