第一步:首先要比较出两个版本之间改变的文件,并把有改变的文件文件路径生成一到一个文件里。这一步我用shell来完成。
这里假设要打796到809后的增量包。把改变的文件路径生成到patchfilepath.txt里面。
svn diff -r 809:796|grep -i Index:|awk -F : '{print $2}' >patchfilepath.txt
第二步用java完成。
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * 这个类用来把两个版本有变化的文件考到指定的目录下 * 在运行这个类前,应该把要打的版本update下来 * @author Alec * */ public class PackageSVN { public static String configPath = "D:/project/workspace/Test/src/com/yan/config.txt";//版本之间有改变的文件的路径列表 public static String baseDir = "D:/project/workspace/ofbiz-9.4new/";//源文件根目录 public static String destDir = "E:/ofbiz_svn/";//目标文件根目录 public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader(configPath)); String s = null; while((s = br.readLine()) != null) { s = s.trim();//去掉路径前面的空格 String str = destDir + s; if(!destDir.equals(str)){//过滤空行 copy(s,destDir); } } br.close(); compress("E:/ofbiz_svn","E:/ofbiz_svn/ofbiz_svn20111102.zip"); } private static void copy(String file, String destbaseDir) throws Exception { String srcFile = baseDir + file; String desFile = destDir + file; int lastIndex = desFile.lastIndexOf("/"); String desPath = desFile.substring(0, lastIndex); File srcF = new File(srcFile); if(srcF.exists()){//如果不存在这样的源文件,就不再拷贝,这个用来解决版本之间有删除文件的情况。 File desF = new File(desFile); File desP = new File(desPath); if(!desP.exists()) { desP.mkdirs(); } System.out.println(srcFile); FileInputStream fis = new FileInputStream(srcF); FileOutputStream fos = new FileOutputStream(desF); byte[] buf = new byte[1024]; int len = 0; while((len = fis.read(buf)) != -1) { fos.write(buf,0,len); } fos.flush(); fos.close(); fis.close(); } } /** *//** * 递归压缩文件 * @param source 源路径,可以是文件,也可以目录 * @param destinct 目标路径,压缩文件名 * @throws IOException */ private static void compress(String source,String destinct) throws IOException { List fileList=loadFilename(new File(source)); ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(destinct))); byte[] buffere=new byte[8192]; int length; BufferedInputStream bis; for(int i=0;i<fileList.size();i++) { File file=(File) fileList.get(i); zos.putNextEntry(new ZipEntry(getEntryName(source,file))); bis=new BufferedInputStream(new FileInputStream(file)); while(true) { length=bis.read(buffere); if(length==-1) break; zos.write(buffere,0,length); } bis.close(); zos.closeEntry(); } zos.close(); } /** *//** * 递归获得该文件下所有文件名(不包括目录名) * @param file * @return */ private static List loadFilename(File file) { List filenameList=new ArrayList(); if(file.isFile()) { filenameList.add(file); } if(file.isDirectory()) { for(File f:file.listFiles()) { filenameList.addAll(loadFilename(f)); } } return filenameList; } /** *//** * 获得zip entry 字符串 * @param base * @param file * @return */ private static String getEntryName(String base,File file) { File baseFile=new File(base); String filename=file.getPath(); //int index=filename.lastIndexOf(baseFile.getName()); if(baseFile.getParentFile().getParentFile()==null) return filename.substring(baseFile.getParent().length()); return filename.substring(baseFile.getParent().length()+1); } }