1. package org.jr.util;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年2月25日
  6. * @author Cherami
  7. */
  8. import java.io.*;
  9. import java.net.*;
  10. import java.util.*;
  11. import java.util.zip.*;
  12. import java.awt.*;
  13. import javax.swing.*;
  14. import org.jr.io.*;
  15. /**
  16. * 简化提取打包在Jar或者Zip文件中的资源。
  17. * 此类提供一些方法更方便的从Jar或者Zip文件中得到资源。
  18. * @since 0.4
  19. */
  20. public final class JarResource {
  21. private HashMap entries = new HashMap();
  22. private HashMap names = new HashMap();
  23. private String fileName;
  24. private ZipFile file;
  25. /**
  26. * 根据指定的文件名创建JarResource。
  27. * @param fileName 文件名
  28. * @since 0.4
  29. */
  30. public JarResource(String fileName) {
  31. this.fileName = fileName;
  32. init();
  33. }
  34. /**
  35. * 根据文件名得到在压缩包中的ZipEntry。
  36. * 可能一个文件名会有多个可能的对应项,具体对应那一项不能确定。对于这样的情况请使用全限定路径。
  37. * @param fileName 文件名
  38. * @return 对应的压缩包中的ZipEntry,不存在时返回null
  39. */
  40. private ZipEntry getEntry(String fileName) {
  41. ZipEntry entry = (ZipEntry) entries.get(fileName);
  42. if (entry == null) {
  43. String entryName = (String) names.get(fileName);
  44. if (entryName != null) {
  45. return (ZipEntry) entries.get(entryName);
  46. }
  47. else {
  48. return null;
  49. }
  50. }
  51. else {
  52. return entry;
  53. }
  54. }
  55. /**
  56. * 提取指定的文件内容并返回一个字节数组。
  57. * @param fileName 资源的文件名
  58. * @return 指定的文件内容的字节数组
  59. * @since 0.4
  60. */
  61. public byte[] getResource(String fileName) {
  62. ZipEntry entry = getEntry(fileName);
  63. if (entry != null) {
  64. try {
  65. InputStream inputStream = file.getInputStream(entry);
  66. int length = inputStream.available();
  67. byte contents[] = new byte[length];
  68. inputStream.read(contents);
  69. inputStream.close();
  70. return contents;
  71. }
  72. catch (IOException e) {
  73. return null;
  74. }
  75. }
  76. else {
  77. return null;
  78. }
  79. }
  80. /**
  81. * 提取指定的文件所代表的图像。
  82. * @param fileName 资源的文件名
  83. * @return 指定的文件所代表的图像
  84. * @since 0.4
  85. */
  86. public Image getImage(String fileName) {
  87. ZipEntry entry = getEntry(fileName);
  88. if (entry != null) {
  89. StringBuffer url = new StringBuffer("jar:file:/");
  90. url.append(FileUtil.getUNIXfilePath(this.fileName));
  91. url.append("!/");
  92. url.append(entry.getName());
  93. try {
  94. URL fileURL = new URL(url.toString());
  95. return new ImageIcon(fileURL).getImage();
  96. }
  97. catch (MalformedURLException e) {
  98. return null;
  99. }
  100. }
  101. else {
  102. return null;
  103. }
  104. }
  105. /**
  106. * 提取指定的文件所代表的字符串。
  107. * @param fileName 资源的文件名
  108. * @return 指定的文件所代表的字符串
  109. * @since 0.4
  110. */
  111. public String getString(String fileName) {
  112. byte contents[] = getResource(fileName);
  113. if (contents != null) {
  114. return new String(contents);
  115. }
  116. else {
  117. return null;
  118. }
  119. }
  120. /**
  121. * 初始化内部的资源项HashMap。
  122. */
  123. private void init() {
  124. try {
  125. file = new ZipFile(fileName);
  126. Enumeration enumeration = file.entries();
  127. while (enumeration.hasMoreElements()) {
  128. ZipEntry entry = (ZipEntry) enumeration.nextElement();
  129. if (!entry.isDirectory()) {
  130. entries.put(entry.getName(), entry);
  131. names.put(FileUtil.getFileName(entry.getName()), entry.getName());
  132. }
  133. }
  134. }
  135. catch (FileNotFoundException e) {
  136. e.printStackTrace();
  137. }
  138. catch (IOException e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. }