解压所在目录下所有zip包 ,对解压的文件编辑内容 ,将编辑完的文件存放至目标目录下_02

时间:2022-12-11 11:29:00
package demo0325;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;


import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;


//解压所在目录下所有zip包 ,对解压的文件编辑内容 ,将编辑完的文件存放至目标目录下
public class addScript {


//在工程目录demo0325生成所有用户可执行的脚本
//zipFileDir="D:\\Android\\jars"
//projectDir="D:\\Android\\workspace\\MqcDemo\\src\\demo0325\\";
//packageContent="package demo0325;"
public static void main(String zipFileDir,String projectDir,String packageContent) throws IOException {
// TODO Auto-generated method stub
UnAllZip(zipFileDir,projectDir,packageContent);
}

//解压所在目录下所有zip包 ,对解压的文件编辑内容 ,将编辑完的文件存放至目标目录下
public static void UnAllZip(String zipFileDir,String projectDir,String packageContent) throws IOException{
File fileFolder=new File(zipFileDir);
String[] fileNames=fileFolder.list();
for(int i=0;i<fileNames.length;i++){
//截取当前文件夹最后四个字符内容
String fileType=fileNames[i].substring(fileNames[i].length()-4);
if(fileType.equals(".zip"))
{
//用户脚本
String scriptName=fileNames[i].substring(0,fileNames[i].length()-4)+".java";
String userJavaScript=zipFileDir+"\\"+scriptName;
//解压当前zip文件
UnZip(zipFileDir+"\\", fileNames[i].toString());
//生成对应目标脚本
CreateProjectScript(scriptName, userJavaScript, packageContent, projectDir);
}
}
}

//解压一个.zip文件到相同目录下 zipFileDir=D:\\Android\\jars\\  zipFileName=Main.zip
  public static void UnZip(String zipFileDir,String zipFileName) throws IOException{
  File file = new File(zipFileDir+zipFileName);
  ZipFile zipFile = null;
  try {
  zipFile = new ZipFile(file,"GBK");//设置编码格式
  } catch (IOException exception) {
  exception.printStackTrace();
  System.out.println("解压文件不存在!");
  }
  Enumeration e = zipFile.getEntries(); 
  while(e.hasMoreElements()) { 
  ZipEntry zipEntry =(ZipEntry)e.nextElement();
  if(zipEntry.isDirectory()){
  String name=zipEntry.getName();
  name=name.substring(0,name.length()-1);
  File f=new File(zipFileDir+name);
  f.mkdirs();
  }else {
  File f = new File(zipFileDir+zipEntry.getName());
  f.getParentFile().mkdirs();
  f.createNewFile();
  InputStream is=zipFile.getInputStream(zipEntry);
  FileOutputStream fos=new FileOutputStream(f);
  int length=0;
  byte[] b=new byte[1024];
  while((length=is.read(b,0,1024))!=-1){
  fos.write(b, 0, length);
  }
  is.close();
  fos.close();
  }
  }
  if(zipFile != null){
  zipFile.close();
  }
  //file.deleteOnExit();
  //System.out.println("解压完"+zipFileName+"包");
  }


  //生成对应目标脚本
  public static void CreateProjectScript (String scriptName, String userJavaScript,String packageContent,String projectDir) throws IOException
  {
List<String> listArr=new ArrayList<String>();
     listArr.add(packageContent);
     listArr.add("\r\n");
     BufferedReader javaFileBR=null;
  try{
  javaFileBR=new BufferedReader(new InputStreamReader(new FileInputStream(userJavaScript),"GBK"));
     String str1="";
     while ((str1=javaFileBR.readLine())!=null) {
  listArr.add(str1);
  }
  }catch(Exception e){
  System.out.println("获取文本出错");
  }finally {
  javaFileBR.close();
  }
  //for(int i=0;i<listArr.size();i++){
  // System.out.println(listArr.get(i).toString());
  //}
 
  File changeFile = new File(projectDir);
  if(!changeFile.exists()){
  changeFile.mkdirs();
 
  // fileName表示你创建的文件名;为.java类型;
  File file = new File(changeFile,scriptName);
  if(!file.exists()){
  try {
  file.createNewFile();
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
  try {  
             FileOutputStream out = new FileOutputStream(projectDir+scriptName);  
             OutputStreamWriter outWriter = new OutputStreamWriter(out, "UTF-8");  
             BufferedWriter bufWrite = new BufferedWriter(outWriter);  
             for (int j=0;j<listArr.size();j++) {  
                 bufWrite.write(listArr.get(j)+"\r\n");  
             }  
             bufWrite.close();  
             outWriter.close();  
             out.close();  
         } catch (Exception e) {  
             e.printStackTrace();  
             System.out.println("读取" + projectDir+scriptName + "出错!");  
     }finally{
      //System.out.println(scriptName+"脚本已添加package行");
     }
  }
}