Java自动从服务器复制文件并且运行可执行文件并打包

  1 package com.prize;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileFilter;
  7 import java.io.FileInputStream;
  8 import java.io.FileNotFoundException;
  9 import java.io.FileOutputStream;
 10 import java.io.IOException;
 11 import java.io.InputStreamReader;
 12 import java.io.RandomAccessFile;
 13 import java.io.Reader;
 14 import java.nio.ByteBuffer;
 15 import java.nio.channels.FileChannel;
 16 import java.util.zip.ZipEntry;
 17 import java.util.zip.ZipOutputStream;
 18 
 19 public class CopyPackaging {
 20     public static boolean APDB1 = false;
 21     public static boolean APDB2 = false;
 22     public static boolean BPLGU1 = false;
 23     public static boolean BPLGU2 = false;
 24     public static boolean SERVERADDRESSRIGHTWRONG = false;
 25     public static String OUTADDRESS = "\\out\\target\\product\\prize6735m_65c_l1";
 26     public static String APDBADDRESS = "\\out\\target\\product\\prize6735m_65c_l1\\obj\\CGEN\\";
 27     public static String APDBFIRST = "APDB_MT6735_S01_L1.MP3_W15.29";
 28     public static String APDBSECOND = "APDB_MT6735_S01_L1.MP3_W15.29_ENUM";
 29     public static String BPLGUADDRESS = "\\out\\target\\product\\prize6735m_65c_l1\\system\\etc\\mddb\\";
 30     public static String BPLGUFIRST = "BPLGUInfoCustomAppSrcP_MT6735_S00_MOLY_LR9_W1444_MD_LWTG_CMCC_MP_V10_P5_1_ltg_n";
 31     public static String BPLGUSECOND = "BPLGUInfoCustomAppSrcP_MT6735_S00_MOLY_LR9_W1444_MD_LWTG_CMCC_MP_V10_P5_1_lwg_n";
 32     public static String PACKAGINGNAME = "";
 33 
 34     public static void main(String args[]) {
 35         try {
 36             serverCopyToLocation("Z:\\dux_pcba-os", "D:\\");
 37         } catch (IOException e) {
 38             e.printStackTrace();
 39         }
 40     }
 41 
 42     // 判断服务器地址正确与否
 43     public static boolean judgeServerAddress(String serverAddress) {
 44         File sf = new File(serverAddress);
 45         int num = sf.listFiles(new FileFilter() {
 46 
 47             @Override
 48             public boolean accept(File pathname) {
 49                 if (pathname.isDirectory() && pathname.getName().equals("out")) {
 50                     return true;
 51                 }
 52                 return false;
 53             }
 54 
 55         }).length;
 56         if (num == 1) {
 57             return true;
 58         }
 59         return false;
 60     }
 61 
 62     // 获取包名
 63     public static void getPackingName(String serverAddress) {
 64         File sf = new File(serverAddress + OUTADDRESS + "\\system\\build.prop");
 65         StringBuffer name = new StringBuffer();
 66         Reader reader = null;
 67 
 68         try {
 69             reader = new InputStreamReader(new FileInputStream(sf));
 70             int tempchar;
 71             while ((tempchar = reader.read()) != -1) {
 72 
 73                 name.append((char) tempchar);
 74 
 75             }
 76             PACKAGINGNAME = name.toString()
 77                     .substring(name.toString().indexOf("ro.build.display.id="),
 78                             name.toString().indexOf("ro.build.version.incremental"))
 79                     .substring(name.toString()
 80                             .substring(name.toString().indexOf("ro.build.display.id=") - 1,
 81                                     name.toString().indexOf("ro.build.version.incremental"))
 82                             .indexOf("="))
 83                     .toString().replace("\n", "");
 84         } catch (FileNotFoundException e) {
 85             e.printStackTrace();
 86         } catch (IOException e) {
 87             e.printStackTrace();
 88         } finally {
 89             if (reader != null) {
 90                 try {
 91                     reader.close();
 92                 } catch (IOException e) {
 93                     // TODO Auto-generated catch block
 94                     e.printStackTrace();
 95                 }
 96             }
 97         }
 98     }
 99 
100     // 从服务器拷贝文件到系统盘符
101     public static void serverCopyToLocation(String serverAddress, String locationAddress) throws IOException {
102         File sf = new File(serverAddress + OUTADDRESS);
103         String locationTarget = locationAddress + "packaging";
104         File nlf = new File(locationTarget);
105         if (!nlf.exists() && !nlf.isDirectory()) {
106             nlf.mkdir();
107         }
108         getPackingName(serverAddress);
109         File[] allSource = sf.listFiles();
110         for (int i = 0; i < allSource.length; i++) {
111             if (!allSource[i].isDirectory()) {
112                 System.out.println("path=" + allSource[i].getAbsolutePath() + "--------" + allSource[i].getName());
113                 nioTransferCopy(new File(allSource[i].getAbsolutePath()),
114                         new File(locationTarget + "\\" + allSource[i].getName()));
115             }
116         }
117 
118         judgeServerOutNeedCopy(serverAddress);
119         if (!APDB1 && new File(serverAddress + APDBADDRESS + APDBFIRST).exists()) {
120             nioTransferCopy(new File(serverAddress + APDBADDRESS + APDBFIRST),
121                     new File(locationTarget + "\\" + APDBFIRST));
122         }
123         if (!APDB2 && new File(serverAddress + APDBADDRESS + APDBSECOND).exists()) {
124             nioTransferCopy(new File(serverAddress + APDBADDRESS + APDBSECOND),
125                     new File(locationTarget + "\\" + APDBSECOND));
126         }
127         if (!BPLGU1 && new File(serverAddress + BPLGUADDRESS + BPLGUFIRST).exists()) {
128             nioTransferCopy(new File(serverAddress + BPLGUADDRESS + BPLGUFIRST),
129                     new File(locationTarget + "\\" + BPLGUFIRST));
130         }
131         if (!BPLGU2 && new File(serverAddress + BPLGUADDRESS + BPLGUSECOND).exists()) {
132             nioTransferCopy(new File(serverAddress + BPLGUADDRESS + BPLGUSECOND),
133                     new File(locationTarget + "\\" + BPLGUSECOND));
134         }
135 
136         nioTransferCopy(new File(System.getProperty("user.dir") + "\\" + "CheckSum_Gen.exe"),
137                 new File(locationTarget + "\\" + "CheckSum_Gen.exe"));
138 
139         nioTransferCopy(new File(System.getProperty("user.dir") + "\\" + "FlashToolLib.dll"),
140                 new File(locationTarget + "\\" + "FlashToolLib.dll"));
141 
142         System.getProperty("user.dir");
143         if (checkSum(System.getProperty("user.dir") + "\\" + "CheckSum_Gen.exe", locationTarget)) {
144             try {
145                 Thread.sleep(20000);
146             } catch (InterruptedException e) {
147                 // TODO Auto-generated catch block
148                 e.printStackTrace();
149             }
150             deleteFile(locationTarget);
151             fileToZip(locationTarget, "D:\\", PACKAGINGNAME);
152         }
153     }
154 
155     // 单线程复制文件最快方法,文件越大越有优势
156     public static void nioTransferCopy(File source, File target) {
157         FileChannel fc = null;
158         FileChannel fco = null;
159         try {
160             fc = new FileInputStream(source).getChannel();
161             fco = new RandomAccessFile(target, "rw").getChannel();
162 
163             ByteBuffer buf = ByteBuffer.allocate(1024);
164 
165             while (fc.read(buf) != -1) {
166                 buf.flip();
167                 fco.write(buf);
168                 buf.clear();
169             }
170         } catch (FileNotFoundException e) {
171             // TODO Auto-generated catch block
172             e.printStackTrace();
173         } catch (IOException e) {
174             // TODO Auto-generated catch block
175             e.printStackTrace();
176         } finally {
177             try {
178                 fc.close();
179                 fco.close();
180             } catch (IOException e) {
181                 // TODO Auto-generated catch block
182                 e.printStackTrace();
183             }
184 
185         }
186 
187     }
188 
189     /**
190      * 判断是否需要负责那4个文件
191      * 
192      * @return
193      */
194     public static void judgeServerOutNeedCopy(String serverAddress) {
195         System.out.println(serverAddress);
196         File f = new File(serverAddress + OUTADDRESS);
197         File[] fPath = f.listFiles();
198         for (int i = 0; i < fPath.length; i++) {
199 
200             if (fPath[i].getName().equals("APDB_MT6735_S01_L1.MP3_W15.29")) {
201                 APDB1 = true;
202             }
203             if (fPath[i].getName().equals("APDB_MT6735_S01_L1.MP3_W15.29_ENUM")) {
204                 APDB2 = true;
205             }
206             if (fPath[i].getName()
207                     .equals("BPLGUInfoCustomAppSrcP_MT6735_S00_MOLY_LR9_W1444_MD_LWTG_CMCC_MP_V10_P5_1_ltg_n")) {
208                 BPLGU1 = true;
209             }
210             if (fPath[i].getName()
211                     .equals("BPLGUInfoCustomAppSrcP_MT6735_S00_MOLY_LR9_W1444_MD_LWTG_CMCC_MP_V10_P5_1_lwg_n")) {
212                 BPLGU2 = true;
213             }
214 
215         }
216     }
217 
218     /**
219      * 执行CheckSum_Gen程序
220      * 
221      * @param checksum
222      *            :可执行文件路径
223      * @param target
224      *            :生成的文件存放路径
225      * @return
226      */
227     public static boolean checkSum(String checksum, String target) {
228         try {
229 
230             String[] ss = { "cmd.exe", "/k", "start", checksum };
231             Runtime.getRuntime().exec(ss, null, new File(target));
232 
233         } catch (Exception e) {
234             e.printStackTrace();
235             System.out.println(e.toString());
236             return false;
237         }
238         return true;
239     }
240 
241     /**
242      * 删除多余的文件
243      * 
244      * @return
245      */
246     public static void deleteFile(String target) {
247         File file = new File(target);
248         File[] showFile = file.listFiles();
249         for (int i = 0; i < showFile.length; i++) {
250             if (showFile[i].getName().equals("android-info.txt") || showFile[i].getName().equals("appsboot.mbn")
251                     || showFile[i].getName().equals("appsboot.raw") || showFile[i].getName().equals("appsboot.raw")
252                     || showFile[i].getName().equals("boot-verified.img")
253                     || showFile[i].getName().equals("CheckSum_Gen.exe")
254                     || showFile[i].getName().equals("clean_steps.mk")
255                     || showFile[i].getName().equals("custom_build_verno")
256                     || showFile[i].getName().equals("installed-files.txt") || showFile[i].getName().equals("kernel")
257                     || showFile[i].getName().equals("lk-verified.bin")
258                     || showFile[i].getName().equals("logo-verified.bin")
259                     || showFile[i].getName().equals("previous_build_config.mk")
260                     || showFile[i].getName().equals("ramdisk.img")
261                     || showFile[i].getName().equals("ramdisk-recovery.img")
262                     || showFile[i].getName().equals("recovery-verified.img")
263                     || showFile[i].getName().equals("secro-verified.img")
264                     || showFile[i].getName().equals("FlashToolLib.dll")
265                     || showFile[i].getName().equals("target_files-package.zip")) {
266                 showFile[i].delete();
267 
268             }
269         }
270     }
271 
272     // 打包
273     /**
274      * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
275      * 
276      * @param sourceFilePath
277      *            :待压缩的文件路径
278      * @param zipFilePath
279      *            :压缩后存放路径
280      * @param fileName
281      *            :压缩后文件的名称
282      * @return
283      */
284     public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
285         boolean flag = false;
286         File sourceFile = new File(sourceFilePath);
287         FileInputStream fis = null;
288         BufferedInputStream bis = null;
289         FileOutputStream fos = null;
290         ZipOutputStream zos = null;
291 
292         if (sourceFile.exists() == false) {
293             System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
294         } else {
295             try {
296                 File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
297                 if (zipFile.exists()) {
298                     System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
299                 } else {
300                     File[] sourceFiles = sourceFile.listFiles();
301                     if (null == sourceFiles || sourceFiles.length < 1) {
302                         System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
303                     } else {
304                         fos = new FileOutputStream(zipFile);
305                         zos = new ZipOutputStream(new BufferedOutputStream(fos));
306                         byte[] bufs = new byte[1024 * 10];
307                         for (int i = 0; i < sourceFiles.length; i++) {
308                             // 创建ZIP实体,并添加进压缩包
309                             ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
310                             zos.putNextEntry(zipEntry);
311                             // 读取待压缩的文件并写进压缩包里
312                             fis = new FileInputStream(sourceFiles[i]);
313                             bis = new BufferedInputStream(fis, 1024 * 10);
314                             int read = 0;
315                             while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
316                                 zos.write(bufs, 0, read);
317                             }
318                         }
319                         flag = true;
320                     }
321                 }
322             } catch (FileNotFoundException e) {
323                 e.printStackTrace();
324                 throw new RuntimeException(e);
325             } catch (IOException e) {
326                 e.printStackTrace();
327                 throw new RuntimeException(e);
328             } finally {
329                 // 关闭流
330                 try {
331                     if (null != bis)
332                         bis.close();
333                     if (null != zos)
334                         zos.close();
335                 } catch (IOException e) {
336                     e.printStackTrace();
337                     throw new RuntimeException(e);
338                 }
339             }
340         }
341         return flag;
342     }
343 }

读取包名的文件system.prop

 1 #
 2 # system.prop for generic sdk
 3 #
 4 
 5 rild.libpath=mtk-ril.so
 6 rild.libargs=-d /dev/ttyC0
 7 
 8 
 9 # MTK, Infinity, 20090720 {
10 wifi.interface=wlan0
11 # MTK, Infinity, 20090720 }
12 
13 # MTK, mtk03034, 20101210 {
14 ro.mediatek.wlan.wsc=1
15 # MTK, mtk03034 20101210}
16 # MTK, mtk03034, 20110318 {
17 ro.mediatek.wlan.p2p=1
18 # MTK, mtk03034 20110318}
19 
20 # MTK, mtk03034, 20101213 {
21 mediatek.wlan.ctia=0
22 # MTK, mtk03034 20101213}
23 
24 
25 #
26 wifi.tethering.interface=ap0
27 #
28 
29 ro.opengles.version=196608
30 #ro.kernel.qemu=1
31 #ro.kernel.qemu.gles=0
32 
33 wifi.direct.interface=p2p0
34 dalvik.vm.heapgrowthlimit=128m
35 dalvik.vm.heapsize=256m
36 
37 # USB MTP WHQL
38 ro.sys.usb.mtp.whql.enable=0
39 
40 # Power off opt in IPO
41 sys.ipo.pwrdncap=2
42 
43 ro.sys.usb.storage.type=mtp,mass_storage
44 
45 # USB BICR function
46 ro.sys.usb.bicr=yes
47 
48 # USB Charge only function
49 ro.sys.usb.charging.only=yes
50 
51 # audio
52 ro.camera.sound.forced=0
53 ro.audio.silent=0
54 
55 ro.zygote.preload.enable=0
56 
57 # temporary enables NAV bar (soft keys)
58 qemu.hw.mainkeys=0
59 
60 ro.kernel.zio=38,108,105,16
61 #ro.kernel.qemu=1
62 #ro.kernel.qemu.gles=0
63 #ro.boot.selinux=disable
64 
65 # Disable dirty region for Mali
66 debug.hwui.render_dirty_regions=false
67 
68 ro.sf.lcd_density=480
69 
70 # performance
71 ro.mtk_perf_simple_start_win=1
72 ro.mtk_perf_response_time=1

 执行结果:

posted @ 2015-08-22 17:03  comkingfly  阅读(2104)  评论(0编辑  收藏  举报