logo

New Response

« Return to the main article

You are replying to:

    • avatar
    • Dweeb
    • Posted on Tue 14 Sep 2004

    This is messy combination of an article from the Sun Java site and Jakes original. It was written to operate as a Web Query Save agent on a doc saved with a zipped attachment (or ZIP in filename = sloppy I know) specifying the 'unzip' directory in the [Target] field.

    Also for simplicity the doc contains a field [AttachmentNames] containing @Attachmentnames.

    import lotus.domino.*; import java.io.*; import java.util.*; import java.util.zip.*;

    public class JavaAgent extends AgentBase { static final int BUFFER = 2048; public void NotesMain() {

    try { Session session = getSession(); AgentContext agentContext = session.getAgentContext(); Document doc = agentContext.getDocumentContext(); String sPath = doc.getItemValueString( "Target"); Vector v = null; if (doc.hasEmbedded()) { System.out.println("\"" + doc.getItemValueString("Subject") + "\" has embedded objects."); v = doc.getEmbeddedObjects(); if (v.isEmpty()){ System.out.println( "\tEmbedded object is an attachment."); v = doc.getItemValue( "AttachmentNames"); for ( int i=0; i< v.size(); i++){ //Collect file and unzip to target directory String sFilename = (String) v.elementAt( i); EmbeddedObject obj = doc.getAttachment( sFilename); String sArchiveName = session.getEnvironmentString("Directory",true) + "\\" + doc.getNoteID() + ".zip"; //If file is zipped then extract if ( sFilename.indexOf( "zip") != -1){

    InputStream iis = obj.getInputStream(); BufferedOutputStream dest = null;

    CheckedInputStream checksum = new CheckedInputStream( iis, new Adler32()); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum)); ZipEntry entry; while((entry = zis.getNextEntry()) != null) { System.out.println("Extracting: " +entry); int count; byte data[] = new byte[BUFFER]; // write the files to the disk FileOutputStream fos = new FileOutputStream( sPath + "/" + entry.getName()); dest = new BufferedOutputStream( fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } zis.close();

    //ZipInputStream zin = new ZipInputStream( iis); //Once a ZIP input stream is opened, you can read the zip entries using the /*

    BufferedOutputStream dest = null; BufferedInputStream is = null; ZipEntry entry; // ZipFile zipfile = new ZipFile(argv[0]); ZipFile zipfile = new ZipFile( sArchiveName); Enumeration e = zipfile.entries(); while(e.hasMoreElements()) { entry = (ZipEntry) e.nextElement(); System.out.println("Extracting: " +entry); is = new BufferedInputStream (zipfile.getInputStream(entry)); int count; byte data[] = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(entry.getName()); dest = new BufferedOutputStream(fos, BUFFER); while ((count = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); is.close(); } */ } } } } } catch(Exception e) { e.printStackTrace(); } } }

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment: