Jelajahi Sumber

修复打成jar包之后无法导出接口的问题

mxd 4 tahun lalu
induk
melakukan
43f98b3fa8

+ 14 - 2
src/main/java/org/ssssssss/magicapi/adapter/Resource.java

@@ -65,14 +65,26 @@ public interface Resource {
 		return false;
 	}
 
+	default String separator(){
+		return null;
+	}
+
 	default void processExport(ZipOutputStream zos, String path, Resource directory, List<Resource> resources, List<String> excludes) throws IOException {
 		for (Resource resource : resources) {
-			if (resource.parent().getAbsolutePath().equals(directory.getAbsolutePath()) && !excludes.contains(resource.name())) {
+			String fullName = directory.getAbsolutePath();
+			if (!fullName.endsWith(separator())) {
+				fullName += separator();
+			}
+			fullName += resource.name();
+			if (resource.isDirectory()) {
+				fullName += separator();
+			}
+			if (fullName.equals(resource.getAbsolutePath()) && !excludes.contains(resource.name())) {
 				if (resource.isDirectory()) {
 					String newPath = path + resource.name() + "/";
 					zos.putNextEntry(new ZipEntry(newPath));
 					zos.closeEntry();
-					processExport(zos, newPath, resource, resource.resources(), excludes);
+					processExport(zos, newPath, resource, resources, excludes);
 				} else {
 					zos.putNextEntry(new ZipEntry(path + resource.name()));
 					zos.write(resource.read());

+ 20 - 0
src/main/java/org/ssssssss/magicapi/adapter/resource/FileResource.java

@@ -4,10 +4,13 @@ import org.ssssssss.magicapi.adapter.Resource;
 import org.ssssssss.magicapi.utils.IoUtils;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 public class FileResource implements Resource {
 
@@ -113,4 +116,21 @@ public class FileResource implements Resource {
 		return String.format("file://%s", this.file.getAbsolutePath());
 	}
 
+	@Override
+	public void processExport(ZipOutputStream zos, String path, Resource directory, List<Resource> resources, List<String> excludes) throws IOException {
+		for (Resource resource : resources) {
+			if (resource.parent().getAbsolutePath().equals(directory.getAbsolutePath()) && !excludes.contains(resource.name())) {
+				if (resource.isDirectory()) {
+					String newPath = path + resource.name() + "/";
+					zos.putNextEntry(new ZipEntry(newPath));
+					zos.closeEntry();
+					processExport(zos, newPath, resource, resource.resources(), excludes);
+				} else {
+					zos.putNextEntry(new ZipEntry(path + resource.name()));
+					zos.write(resource.read());
+					zos.closeEntry();
+				}
+			}
+		}
+	}
 }

+ 11 - 2
src/main/java/org/ssssssss/magicapi/adapter/resource/JarResource.java

@@ -40,6 +40,11 @@ public class JarResource implements Resource {
 		this.parent = parent;
 	}
 
+	@Override
+	public String separator() {
+		return "/";
+	}
+
 	@Override
 	public boolean readonly() {
 		return true;
@@ -82,8 +87,12 @@ public class JarResource implements Resource {
 
 	@Override
 	public String name() {
-		int index = this.entryName.lastIndexOf("/");
-		return index > -1 ? this.entryName.substring(index) : this.entryName;
+		String name = this.entryName;
+		if (isDirectory()) {
+			name = name.substring(0, name.length() - 1);
+		}
+		int index = name.lastIndexOf("/");
+		return index > -1 ? name.substring(index + 1) : name;
 	}
 
 	@Override

+ 5 - 25
src/main/java/org/ssssssss/magicapi/adapter/resource/KeyValueResource.java

@@ -33,6 +33,11 @@ public abstract class KeyValueResource implements Resource {
 		this.readonly = readonly;
 	}
 
+	@Override
+	public String separator() {
+		return this.separator;
+	}
+
 	@Override
 	public boolean isDirectory() {
 		return this.path.endsWith(separator);
@@ -136,29 +141,4 @@ public abstract class KeyValueResource implements Resource {
 		return this.path;
 	}
 
-	@Override
-	public void processExport(ZipOutputStream zos, String path, Resource directory, List<Resource> resources, List<String> excludes) throws IOException {
-		for (Resource resource : resources) {
-			String fullName = directory.getAbsolutePath();
-			if (!fullName.endsWith(separator)) {
-				fullName += separator;
-			}
-			fullName += resource.name();
-			if (resource.isDirectory()) {
-				fullName += separator;
-			}
-			if (fullName.equals(resource.getAbsolutePath()) && !excludes.contains(resource.name())) {
-				if (resource.isDirectory()) {
-					String newPath = path + resource.name() + "/";
-					zos.putNextEntry(new ZipEntry(newPath));
-					zos.closeEntry();
-					processExport(zos, newPath, resource, resources, excludes);
-				} else {
-					zos.putNextEntry(new ZipEntry(path + resource.name()));
-					zos.write(resource.read());
-					zos.closeEntry();
-				}
-			}
-		}
-	}
 }