Resource.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package org.ssssssss.magicapi.adapter;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8. public interface Resource {
  9. /**
  10. * 是否是只读
  11. */
  12. default boolean readonly() {
  13. return false;
  14. }
  15. /**
  16. * 是否存在
  17. */
  18. default boolean exists() {
  19. return false;
  20. }
  21. /**
  22. * 是否是目录
  23. */
  24. default boolean isDirectory() {
  25. return false;
  26. }
  27. /**
  28. * 删除
  29. */
  30. default boolean delete() {
  31. return false;
  32. }
  33. /**
  34. * 创建目录
  35. */
  36. default boolean mkdir() {
  37. return false;
  38. }
  39. /**
  40. * 重命名
  41. */
  42. default boolean renameTo(Resource resource) {
  43. return false;
  44. }
  45. /**
  46. * 写入
  47. */
  48. default boolean write(String content) {
  49. return false;
  50. }
  51. /**
  52. * 写入
  53. */
  54. default boolean write(byte[] bytes) {
  55. return false;
  56. }
  57. default String separator(){
  58. return null;
  59. }
  60. default void processExport(ZipOutputStream zos, String path, Resource directory, List<Resource> resources, List<String> excludes) throws IOException {
  61. for (Resource resource : resources) {
  62. String fullName = directory.getAbsolutePath();
  63. if (!fullName.endsWith(separator())) {
  64. fullName += separator();
  65. }
  66. fullName += resource.name();
  67. if (resource.isDirectory()) {
  68. fullName += separator();
  69. }
  70. if (fullName.equals(resource.getAbsolutePath()) && !excludes.contains(resource.name())) {
  71. if (resource.isDirectory()) {
  72. String newPath = path + resource.name() + "/";
  73. zos.putNextEntry(new ZipEntry(newPath));
  74. zos.closeEntry();
  75. processExport(zos, newPath, resource, resources, excludes);
  76. } else {
  77. zos.putNextEntry(new ZipEntry(path + resource.name()));
  78. zos.write(resource.read());
  79. zos.closeEntry();
  80. }
  81. }
  82. }
  83. }
  84. default void export(OutputStream os, String... excludes) throws IOException {
  85. ZipOutputStream zos = new ZipOutputStream(os);
  86. processExport(zos, "", this, resources(), Arrays.asList(excludes == null ? new String[0] : excludes));
  87. zos.close();
  88. }
  89. /**
  90. * 读取
  91. */
  92. byte[] read();
  93. /**
  94. * 读取当前资源下的所有内容,主要是缓存作用。
  95. */
  96. default void readAll() {
  97. }
  98. /**
  99. * 获取子目录
  100. */
  101. default Resource getDirectory(String name) {
  102. return getResource(name);
  103. }
  104. /**
  105. * 获取子资源
  106. */
  107. Resource getResource(String name);
  108. /**
  109. * 获取资源名
  110. */
  111. String name();
  112. /**
  113. * 获取子资源集合
  114. */
  115. List<Resource> resources();
  116. /**
  117. * 父级资源
  118. */
  119. Resource parent();
  120. /**
  121. * 目录
  122. */
  123. List<Resource> dirs();
  124. /**
  125. * 遍历文件
  126. */
  127. List<Resource> files(String suffix);
  128. /**
  129. * 获取所在位置
  130. */
  131. String getAbsolutePath();
  132. }