Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. static {
  2. try {
  3. System.loadLibrary("crypt"); // used for tests. This library in classpath only
  4. } catch (UnsatisfiedLinkError e) {
  5. try {
  6. NativeUtils.loadLibraryFromJar("/natives/crypt.dll"); // during runtime. .DLL within .JAR
  7. } catch (IOException e1) {
  8. throw new RuntimeException(e1);
  9. }
  10. }
  11. }
  12.  
  13. import java.io.Closeable;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.io.UnsupportedEncodingException;
  21. import java.net.URI;
  22. import java.net.URISyntaxException;
  23. import java.net.URL;
  24. import java.net.URLDecoder;
  25. import java.security.CodeSource;
  26. import java.security.ProtectionDomain;
  27. import java.util.zip.ZipEntry;
  28. import java.util.zip.ZipException;
  29. import java.util.zip.ZipFile;
  30.  
  31.  
  32. public class FileUtils
  33. {
  34. public static String getFileName(final Class<?> owner,
  35. final String name)
  36. throws URISyntaxException,
  37. ZipException,
  38. IOException
  39. {
  40. String fileName;
  41. final URI uri;
  42.  
  43. try
  44. {
  45. final String external;
  46. final String decoded;
  47. final int pos;
  48.  
  49. uri = getResourceAsURI(owner.getPackage().getName().replaceAll("\.", "/") + "/" + name, owner);
  50. external = uri.toURL().toExternalForm();
  51. decoded = external; // URLDecoder.decode(external, "UTF-8");
  52. pos = decoded.indexOf(":/");
  53. fileName = decoded.substring(pos + 1);
  54. }
  55. catch(final FileNotFoundException ex)
  56. {
  57. fileName = null;
  58. }
  59.  
  60. if(fileName == null || !(new File(fileName).exists()))
  61. {
  62. fileName = getFileNameX(owner, name);
  63. }
  64.  
  65. return (fileName);
  66. }
  67.  
  68. private static String getFileNameX(final Class<?> clazz, final String name)
  69. throws UnsupportedEncodingException
  70. {
  71. final URL url;
  72. final String fileName;
  73.  
  74. url = clazz.getResource(name);
  75.  
  76. if(url == null)
  77. {
  78. fileName = name;
  79. }
  80. else
  81. {
  82. final String decoded;
  83. final int pos;
  84.  
  85. decoded = URLDecoder.decode(url.toExternalForm(), "UTF-8");
  86. pos = decoded.indexOf(":/");
  87. fileName = decoded.substring(pos + 1);
  88. }
  89.  
  90. return (fileName);
  91. }
  92.  
  93. private static URI getResourceAsURI(final String resourceName,
  94. final Class<?> clazz)
  95. throws URISyntaxException,
  96. ZipException,
  97. IOException
  98. {
  99. final URI uri;
  100. final URI resourceURI;
  101.  
  102. uri = getJarURI(clazz);
  103. resourceURI = getFile(uri, resourceName);
  104.  
  105. return (resourceURI);
  106. }
  107.  
  108. private static URI getJarURI(final Class<?> clazz)
  109. throws URISyntaxException
  110. {
  111. final ProtectionDomain domain;
  112. final CodeSource source;
  113. final URL url;
  114. final URI uri;
  115.  
  116. domain = clazz.getProtectionDomain();
  117. source = domain.getCodeSource();
  118. url = source.getLocation();
  119. uri = url.toURI();
  120.  
  121. return (uri);
  122. }
  123.  
  124. private static URI getFile(final URI where,
  125. final String fileName)
  126. throws ZipException,
  127. IOException
  128. {
  129. final File location;
  130. final URI fileURI;
  131.  
  132. location = new File(where);
  133.  
  134. // not in a JAR, just return the path on disk
  135. if(location.isDirectory())
  136. {
  137. fileURI = URI.create(where.toString() + fileName);
  138. }
  139. else
  140. {
  141. final ZipFile zipFile;
  142.  
  143. zipFile = new ZipFile(location);
  144.  
  145. try
  146. {
  147. fileURI = extract(zipFile, fileName);
  148. }
  149. finally
  150. {
  151. zipFile.close();
  152. }
  153. }
  154.  
  155. return (fileURI);
  156. }
  157.  
  158. private static URI extract(final ZipFile zipFile,
  159. final String fileName)
  160. throws IOException
  161. {
  162. final File tempFile;
  163. final ZipEntry entry;
  164. final InputStream zipStream;
  165. OutputStream fileStream;
  166.  
  167. tempFile = File.createTempFile(fileName.replace("/", ""), Long.toString(System.currentTimeMillis()));
  168. tempFile.deleteOnExit();
  169. entry = zipFile.getEntry(fileName);
  170.  
  171. if(entry == null)
  172. {
  173. throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
  174. }
  175.  
  176. zipStream = zipFile.getInputStream(entry);
  177. fileStream = null;
  178.  
  179. try
  180. {
  181. final byte[] buf;
  182. int i;
  183.  
  184. fileStream = new FileOutputStream(tempFile);
  185. buf = new byte[1024];
  186. i = 0;
  187.  
  188. while((i = zipStream.read(buf)) != -1)
  189. {
  190. fileStream.write(buf, 0, i);
  191. }
  192. }
  193. finally
  194. {
  195. close(zipStream);
  196. close(fileStream);
  197. }
  198.  
  199. return (tempFile.toURI());
  200. }
  201.  
  202. private static void close(final Closeable stream)
  203. {
  204. if(stream != null)
  205. {
  206. try
  207. {
  208. stream.close();
  209. }
  210. catch(final IOException ex)
  211. {
  212. ex.printStackTrace();
  213. }
  214. }
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement