工具下载
经常遇到修改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();
}
}
* 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;
}
{
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 源代码工程:源代码下载
本博客声明:本人的技术探索过程中,得到了国信司南公司领导方面支持。今后,本人博客里的所有技术探索成果将归“无痕客”、“国信司南”和“博客园”三方共同所有,原创作品如需转载,请注明本博客声明。