I want to build an java application where my code take package.xml and retrieve all metadata. Well this one is done using Java Example code given by Salesforce. Now I need to unzip that zip and do changes in metadata programatically like set Validation, Workflows to Inactive. and then zip the file back with base64 encode.
I used programatically zipped file and deploy it to SFDC using file base Metadata deploy call. Server gives success message but changes are not reflecting in SFDC org.
According to my analysis the issue is with editing the metadata and creating zip file. Looks some encoding issue. Below is the code I use to create zip.
< package wsc;
import org.apache.commons.codec.binary.Base64;
public class ZipUtils {
private List <String> fileList;
public static String OUTPUT_ZIP_FILE;
public static String SOURCE_FOLDER ; // SourceFolder path
public ZipUtils(String SourceFolder, String TargetZip) {
fileList = new ArrayList < String > ();
OUTPUT_ZIP_FILE = TargetZip;
SOURCE_FOLDER = SourceFolder;
}
public static void main(String[] args) {
// ZipUtils appZip = new ZipUtils();
// appZip.generateFileList(new File(SOURCE_FOLDER));
// appZip.zipIt(OUTPUT_ZIP_FILE);
}
public void zipIt(String zipFile) {
byte[] buffer = new byte[1024];
String source = new File(SOURCE_FOLDER).getName();
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
System.out.println("Output to Zip : " + zipFile);
FileInputStream in = null;
for (String file: this.fileList) {
System.out.println("File Added : " + file);
// ZipEntry ze = new ZipEntry(source + File.separator + file);
ZipEntry ze = new ZipEntry(file);
zos.putNextEntry(ze);
try {
in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
// in = new FileInputStream(file);
int len;
while ((len = in .read(buffer)) > 0) {
byte [] enc = Base64.encodeBase64(Arrays.copyOf(buffer, len));
zos.write(enc, 0, enc.length);
}
} 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();
}
}
}
public void generateFileList(File node) {
// add file only
if (node.isFile()) {
fileList.add(generateZipEntry(node.toString()));
}
System.out.println("fileList---"+fileList);
if (node.isDirectory()) {
String[] subNote = node.list();
for (String filename: subNote) {
generateFileList(new File(node, filename));
}
}
}
private String generateZipEntry(String file) {
System.out.println("file---"+file);
System.out.println("SourceFolder---"+SOURCE_FOLDER);
return file.substring(SOURCE_FOLDER.length() + 1, file.length());
}}
Appreciate any help or suggestions. Thanks in Advance, Jitendra