1
16package org.tp23.antinstaller.selfextract;
17
18import java.io.BufferedReader;
19import java.io.File;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.InputStreamReader;
24
30public class ResourceExtractor {
31
32
37 public void copyResource(String resource, File dest, String regex, String replace) throws IOException{
38 InputStream is = this.getClass().getResourceAsStream(resource);
39 if(is == null){
40 throw new IOException("Can not find resource: " + resource);
41 }
42 InputStreamReader isr = new InputStreamReader(is);
43 BufferedReader br = new BufferedReader(isr);
44 FileOutputStream bos = new FileOutputStream(dest);
45 String line = null;
46 while((line = br.readLine()) != null){
47 bos.write(line.replaceAll(regex, replace).getBytes());
48 bos.write(System.getProperty("line.separator").getBytes());
49 }
50 bos.flush();
51 bos.close();
52 br.close();
53 }
54
59 public void copyResourceBinary(String resource, File dest) throws IOException{
60 InputStream is = this.getClass().getResourceAsStream(resource);
61 if(is == null){
62 throw new IOException("Can not find resource: " + resource);
63 }
64 FileOutputStream bos = new FileOutputStream(dest);
65 byte[] buffer = new byte[1024];
66 for(int read = 0; (read = is.read(buffer)) > 0; ){
67 bos.write(buffer, 0, read);
68 }
69 bos.flush();
70 bos.close();
71 is.close();
72 }
73
74}
75