I'm trying to use java tool for deployment and seems I having issue with creating zip file.I wrote function that receive list of files path and build ZIP folder with them.
Issue is when I deploy the zip file, it only running test classes. The deplyment components show 0/0. So I tried the following - -unzip all the files from the zip that created by code. -Zip the files manually to other zip (by mouse right click) With the new zip then java process is working. Therefore I assume there is something wrong with the way I creating the ZIP file.
Any suggestion?
public void createZIP(ArrayList<String> files, String zipFileName){
Map<String, ArrayList<String>> m_type_childs = new HashMap<String, ArrayList<String>>();
//First loop get all relevant types
for(String fileURL : files){
String fileAfterBase = fileURL.substring(fileURL.indexOf("src") + 4);
String fileFolder = fileAfterBase.substring(0, fileAfterBase.indexOf("\\"));
String fileName = fileAfterBase.substring(fileAfterBase.lastIndexOf("\\") + 1, fileAfterBase.lastIndexOf("."));
String sfType = Params.folder_SFType_Map.get(fileFolder);
//For Classes/Pages/Triggers/Components should ignore the metadata-xml file when building the package.xml
if((sfType.equals("ApexClass") || sfType.equals("ApexComponent") || sfType.equals("ApexPage") || sfType.equals("ApexTrigger"))
&& (fileURL.endsWith("meta.xml"))){
continue;
}
if(! m_type_childs.containsKey(sfType)){
m_type_childs.put(sfType, new ArrayList<>());
}
m_type_childs.get(sfType).add(fileName);
}
//Build XML file
String xmlComponentTypes = "";
for(String type : m_type_childs.keySet()){
String membersContent = "";
for(String component : m_type_childs.get(type)){
membersContent += "<members>" + component + "</members>";
}
xmlComponentTypes += "<types>" + membersContent + "<name>" + type + "</name></types>";
}
String xmlFull ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<Package xmlns=\"http://soap.sforce.com/2006/04/metadata\">"+
xmlComponentTypes +
"<version>" + Params.properties.getProperty("PACAKGE_API") + ".0</version>"+
"</Package>";
//Build ZIP File
//For writing to ZIP
byte[] buffer = new byte[1024];
FileOutputStream fos = null;
ZipOutputStream zos = null;
try{
fos = new FileOutputStream(Params.properties.getProperty("ZIP_FILES_FOLDER") + "\\" + (zipFileName.endsWith(".zip") ? zipFileName : zipFileName + ".zip"));
zos = new ZipOutputStream(fos);
FileInputStream in = null;
int len;
//Add the pacakge.xml file
File pacakgeXMLFile = new File(MANIFEST_FILE);
FileWriter fw = new FileWriter(pacakgeXMLFile);
fw.write(xmlFull);
fw.close();
ZipEntry ze = new ZipEntry("unpackaged\\" + MANIFEST_FILE);
zos.putNextEntry(ze);
in = new FileInputStream(MANIFEST_FILE);
while ((len = in .read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
//Second loop - add the files to ZIP
for(String fileURL : files){
String fileAfterBase = fileURL.substring(fileURL.indexOf("src") + 4);
String fileFolder = fileAfterBase.substring(0, fileAfterBase.indexOf("\\"));
String fileName = fileAfterBase.substring(fileAfterBase.lastIndexOf("\\") + 1);
//Add to ZIP file
ZipEntry componentEntry = new ZipEntry("unpackaged\\" + fileFolder + File.separator + fileName);
zos.putNextEntry(componentEntry);
try {
in = new FileInputStream(fileURL);
while ((len = in .read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
} finally {
in.close();
}
}
zos.closeEntry();
System.out.println("Folder successfully compressed");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}