获取zip包中的jar。然后将zip包中的jar拷贝在指定目录。个人demo

来源:互联网 发布:修改网卡mac的软件 编辑:程序博客网 时间:2024/06/05 17:13
@Overrideprotected boolean unpackSoFile(File objFile, String directory) {try{List<String> list=new ArrayList<String>();//解压zipZipFile zipFile = new ZipFile(objFile,Charset.forName("GBK"));copyFileToDirectory(zipFile,directory);Enumeration<?> entries = zipFile.entries();  while(entries.hasMoreElements()){ZipEntry zipEntry=(ZipEntry)entries.nextElement();String entryName=zipEntry.getName();if(entryName.endsWith(".jar")){JarFile jarFile = new JarFile((directory  +File.separator+ entryName).replaceAll("\\*", "/"));Enumeration<?> fileList = jarFile.entries();while (fileList .hasMoreElements()) {JarEntry jarEntry = (JarEntry)fileList.nextElement();String jarEntryName=jarEntry.getName().replaceAll("/", ".");if(jarEntryName.endsWith(".class")&&Class.forName(jarEntryName.substring(0, jarEntryName.length()-6)).newInstance() instanceof  LifeCycle){ list.add(jarEntryName.substring(0, jarEntryName.length()-6));}}jarFile.close();}}}catch(Exception e){log.info("上传到指定目录异常:{}",e);return false;}return true;}


private  void  copyFileToDirectory(ZipFile zipFile, String directory){InputStream in=null;FileOutputStream out=null;try {        for (Enumeration<? extends ZipEntry> entries = zipFile.entries(); entries.hasMoreElements();) {              ZipEntry entry = (ZipEntry) entries.nextElement();              String zipEntryName = entry.getName();              in = zipFile.getInputStream(entry);              out = new FileOutputStream(directory + File.separator +zipEntryName);              byte[] buf = new byte[1024];              int len;              while ((len = in.read(buf)) > 0) {                  out.write(buf, 0, len);              }          }          log.info("******************拷贝完毕********************");  } catch (IOException e) {log.info("解压到指定目录异常,{}",e);}finally{try{in.close();              out.close();}catch(Exception e){log.info("关闭流异常:{}",e);}}}

                ZipsoRegisty zz= new ZipsoRegisty();zz.unpackSoFile(new File("E:/113-v1.0.1.zip"), "F:/");




原创粉丝点击