77 lines
2.4 KiB
Java
77 lines
2.4 KiB
Java
package cz.trask.migration.impl.v32;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
import cz.trask.migration.model.FileType;
|
|
import cz.trask.migration.model.ZipEntryData;
|
|
|
|
public class ZipUtils {
|
|
|
|
private static final Logger log = LogManager.getLogger(ZipUtils.class);
|
|
|
|
public static List<ZipEntryData> extractFilesFromZip(byte[] data) throws IOException {
|
|
List<ZipEntryData> fileList = new ArrayList<>();
|
|
|
|
try (ByteArrayInputStream fis = new ByteArrayInputStream(data); ZipInputStream zis = new ZipInputStream(fis)) {
|
|
|
|
ZipEntry entry;
|
|
while ((entry = zis.getNextEntry()) != null) {
|
|
// Ignoruj adresáře
|
|
if (!entry.isDirectory()) {
|
|
String name = entry.getName().substring(entry.getName().lastIndexOf('/') + 1);
|
|
FileType type = determineFileType(entry.getName());
|
|
byte[] content = readAllBytes(zis);
|
|
|
|
fileList.add(new ZipEntryData(name, type, content));
|
|
}
|
|
zis.closeEntry();
|
|
}
|
|
}
|
|
|
|
return fileList;
|
|
}
|
|
|
|
private static FileType determineFileType(String fileName) {
|
|
String lowerFileName = fileName.toLowerCase();
|
|
if (lowerFileName.endsWith("/meta-information/api.yaml")) {
|
|
return FileType.APIDEF;
|
|
} else if (lowerFileName.endsWith("/meta-information/swagger.yaml")) {
|
|
return FileType.OPENAPI;
|
|
} else if (lowerFileName.endsWith(".wsdl")) {
|
|
return FileType.WSDL;
|
|
} else if (lowerFileName.contains("/sequences/in-sequence")) {
|
|
return FileType.POLICY_IN;
|
|
} else if (lowerFileName.contains("/sequences/out-sequence")) {
|
|
return FileType.POLICY_OUT;
|
|
} else if (lowerFileName.contains("/sequences/fault-sequence")) {
|
|
return FileType.POLICY_FAULT;
|
|
} else if (lowerFileName.endsWith("/meta-information/endpoint_certificates.yaml")) {
|
|
return FileType.CERTIFICATE;
|
|
} else if (lowerFileName.contains("/docs/")) {
|
|
return FileType.DOCUMENTATION;
|
|
|
|
}
|
|
return FileType.UNKNOWN;
|
|
}
|
|
|
|
private static byte[] readAllBytes(ZipInputStream zis) throws IOException {
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
int nRead;
|
|
byte[] data = new byte[1024];
|
|
while ((nRead = zis.read(data, 0, data.length)) != -1) {
|
|
buffer.write(data, 0, nRead);
|
|
}
|
|
buffer.flush();
|
|
return buffer.toByteArray();
|
|
}
|
|
}
|