Search code examples
javawebmethod

Exception while trying to zip files


I have a little issue with a Java service that I call in a Webmethod Flow. I call it with 2 args filepath and filename and retrieve the archive's filepath. This service is supposed to be call when I add files to a specific directory (using a filepolling). And everytime I try to pass several files in the folder I get this exception for the 1st file to be treated:

com.wm.app.b2b.server.ServiceException: 2.null
    at ma.sap.aladin.catalog.in.priv.utils.zip(utils.java:86)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ......

Here's the code of my zip service:

    import com.wm.data.*;
    import com.wm.util.Values;
    import com.wm.app.b2b.server.Service;
    import com.wm.app.b2b.server.ServiceException;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    import com.wm.data.IData;
    import com.wm.data.IDataCursor;
    import com.wm.data.IDataUtil;
    
    public final class zip_SVC

{

    /** 
     * The primary method for the Java service
     *
     * @param pipeline
     *            The IData pipeline
     * @throws ServiceException
     */
    public static final void zip(IData pipeline) throws ServiceException {
        IDataCursor pipelineCursor = pipeline.getCursor();
        String  targetFolder = IDataUtil.getString( pipelineCursor, "targetFolder" );
        String  fileName = IDataUtil.getString( pipelineCursor, "fileName" );
        pipelineCursor.destroy();
        // pipeline
        
        if (targetFolder.charAt(targetFolder.length()-1)!='\\') targetFolder+="\\";
        
        ZipOutputStream zipOut = null;
        FileOutputStream fos = null;        
        FileInputStream fis = null;
        
        String pathPieces[]=fileName.split("\\\\");
        String nameWoPath=pathPieces[pathPieces.length-1].substring(12);
        
        String zipName=targetFolder+nameWoPath+"_zip.zip";
        
        try {
        
            fos = new FileOutputStream(zipName);
            zipOut = new ZipOutputStream(fos);
            File fileToZip = new File(fileName);
            fis = new FileInputStream(fileToZip);
            ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
            zipOut.putNextEntry(zipEntry);
            byte[] bytes = new byte[1024];
            int length;
        while((length = fis.read(bytes)) >= 0) zipOut.write(bytes,0,length);
        
        } catch (Exception e) {
            e.printStackTrace();
        throw new ServiceException("1."+e.getMessage());
        } finally {
            try {
                zipOut.finish();
                zipOut.flush();
                
                fis.close();
                fos.close();    
                zipOut.close();
            } catch (Exception e) {
                e.printStackTrace();
                throw new ServiceException("2."+e.getMessage());
            }
        }
        
        // pipeline
        IDataCursor pipelineCursor_1 = pipeline.getCursor();
        IDataUtil.put( pipelineCursor_1, "zipName", zipName);
        pipelineCursor.destroy();
            
    }

Does anybody have an idea about what happened here ? Thanks in advance :)


Solution

  • I finally found out what the problem was, it seems that my java service was sometimes called before the end of the previous process, which meant that my filename was equal to null and was causing all the other problems, I solved it very simply by calling Thread.sleep(500) just before initializing all my streams.