經常遇到修改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 源代碼工程:源代碼下載
本博客聲明:本人的技術探索過程中,得到了國信司南公司領導方面支持。今后,本人博客里的所有技術探索成果將歸“無痕客”、“國信司南”和“博客園”三方共同所有,原創作品如需轉載,請注明本博客聲明。