0

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();
            }
        }

    }
  • what's a structure inside? Please, check what option singlePackage. In case if that is true, then folder structure should be /src/..., otherwise is should be somepackage/src/... inside zip – kurunve Jul 5 '17 at 11:15
  • Hi structure is in the zip folder 'unpackaged', and inside there is pacakge.xml file + folder per each type (classes, pages, etc....). I think the structure is fine, as if I unzip the file that generated, and zip it again manually, then it's working. – Liron C Jul 5 '17 at 11:18
  • can you share an example of zip file? with some dummy metadata? – kurunve Jul 5 '17 at 11:21
  • How can I upload zip file to my comment? – Liron C Jul 5 '17 at 11:26
  • you can not, upload it somewhere overcloud and share by link – kurunve Jul 5 '17 at 12:27
0

I solved the issue by using solution in this post: https://stackoverflow.com/questions/26743773/how-can-i-make-my-java-generated-zip-file-identical-to-a-winzip-generated-one

Using org.apache.commons.compress.archivers for compressing the zip, instead of java java.util.zip and this is working.

Don't know why exacly, but I can see the properties in the zip are different when generating with each of the libararies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.