做網頁游戲開發的時候,經常會觸及到對文件版本號的管理。
最近由於做新項目的原因,把原來手寫版本號的方法改進了一下,借由svn的版本號生成及用java寫了個xml解析輸出文件,把手動的東西都變成全自動。
主要是思路是這樣的:
1.用svn腳本生成版本的xml文件,這里要用到svn工具,這個是官方的下載地址:http://subversion.apache.org/packages.html
2.對生成的xml文件(如svn.xml)進行解析並打包。輸出個二進制文件(如svn.dat)
3.工具里對版本號文件進行壓縮。
下面貼出來部分關鍵的代碼:
生成svn版本號的腳本
svn status [path] -v --xml > c:\svn.xml
主代碼,打包輸出並壓縮幾個步驟在這里
//獲取XML Document documet = GetDocument(path); //解析 HashMap<String, Integer> map = readFromDocument(documet); //輸出二進制文件 outputBinary(map, outPath); //壓縮 compress(outPath);
解析svn版本號文件
//獲取主路徑 Element element = (Element)documet.getElementsByTagName("target").item(0); mainPath = element.getAttribute("path") + "\\"; mainPath = mainPath.replaceAll("\\\\", "\\\\\\\\");; HashMap<String, Integer> map = new HashMap<>(); NodeList list = documet.getElementsByTagName("entry"); for(int i = 0; i < list.getLength(); i++) { element = (Element)list.item(i); String path = element.getAttribute("path"); File file = new File(path); if(file.isFile()) { if(path.length() != mainPath.length()) path = path.replaceFirst(mainPath, ""); //wc-status commit revision Element verEle = (Element)(element.getElementsByTagName("commit").item(0)); Integer version = Integer.parseInt( verEle.getAttribute("revision") ); System.out.println(path + ":" + version); map.put(path, version); } } return map;
壓縮輸出的文件
System.out.println("#start to compress " + path); BufferedInputStream in = new BufferedInputStream(new FileInputStream(path)); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] temp = new byte[1024]; int size = 0; while ((size = in.read(temp)) != -1) { out.write(temp, 0, size); } in.close(); byte[] data = out.toByteArray(); byte[] output = CompressionUtil.compress(data, CompressionUtil.Level.BEST_COMPRESSION); System.out.println("before : " + (data.length ) + "byte"); /// 1024 System.out.println("after : " + (output.length ) + "byte"); // 1024 FileOutputStream fos = new FileOutputStream(path); fos.write(output); out.close(); fos.close();
整個工具的代碼在這里 http://files.cnblogs.com/yans/VersionPacking.rar
調用java去打包的腳本
java -jar VersionPacking.jar [path] [outputfile]
其中path是svn.xml的路徑, outputfile是輸出的目錄