将文件夹压缩为jar包——JAVA小工具

工具下载

经常遇到修改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 源代码工程:源代码下载

 

本博客声明:本人的技术探索过程中,得到了国信司南公司领导方面支持。今后,本人博客里的所有技术探索成果将归“无痕客”、“国信司南”和“博客园”三方共同所有,原创作品如需转载,请注明本博客声明。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值