////////////////////////////////////////////////////////////////////////////////
/////////
// Agent written by Erwing van Hunen
// Extended by Christoph Berger, 04.02.2002
// The following agent loops through all unprocessed (selected) documents, and
zips all
// attachments unless they have the ending *.zip.
// To mark the processed documents, the agent adds in the subject field the
text
// "<contains zipped files at the end>"
////////////////////////////////////////////////////////////////////////////////
/////////
// standard notes class
public class JavaAgent extends AgentBase {
// main programm
public void NotesMain() {
try {
// declaration of some variables
FileOutputStream fos;
RichTextItem rtitem;
String attachmentname;
String archivename;
String oldarchive;
byte b[] = new byte[512];
// initialize
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database db = agentContext.getCurrentDatabase();
DocumentCollection col = agentContext.getUnprocessedDocuments();
Document doc = col.getFirstDocument();
// loop through all documents
while (doc != null)
{
// save flag
boolean saveFlag = false;
// location of the data dir
archivename = session.getEnvironmentString("Directory",true) + "\\" +
doc.getNoteID() + ".zip";
// create an new file and zip output stream
fos = new FileOutputStream(archivename);
ZipOutputStream zout = new ZipOutputStream(fos);
// get the bodyfield
rtitem = (RichTextItem) doc.getFirstItem("Body");
// get all embedded objects and the number
Vector v = rtitem.getEmbeddedObjects();
Enumeration e = v.elements();
// loop through all objects
while (e.hasMoreElements())
{
EmbeddedObject eo = (EmbeddedObject)e.nextElement();
// check if it's an attachment
if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT)
{
// check the ending of the attachment, if it's a zip file, then it
doesn't make sense to zip it again
if
( eo.getName().toUpperCase().endsWith( ".ZIP" ));
else
{
InputStream in = eo.getInputStream();
ZipEntry ze = new
ZipEntry(eo.getName().replace(File.separatorChar,'/'));
zout.putNextEntry(ze);
int len=0;
// read the bytes
while((len=in.read(b)) != -1)
{
zout.write(b,0,len);
}
// close the entry and remove the attachment
zout.closeEntry();
eo.remove();
saveFlag = true;
}
}
}
// check if something has changed in the document
if (saveFlag)
{
// close the file- and zip output stream
zout.close();
fos.close();
// add some lines to the body item
rtitem.addNewLine( 3 );
// attach the file
rtitem.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null, archivename,
archivename);
// add some text to the subject line
doc.replaceItemValue("Subject", doc.getItemValueString("Subject") + "
<contains zipped files>");
// save the document
doc.save(true, true);
saveFlag = false;
}
// close and delete the zip file
fos.close();
File zipfile = new File(archivename);
zipfile.delete();
// get the next document
doc = col.getNextDocument(doc);
}
// handle the errors
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hi Erwin
I rewrote your code and now it works.
Regards Chris
Here's the code:
//////////////////////////////////////////////////////////////////////////////// ///////// // Agent written by Erwing van Hunen // Extended by Christoph Berger, 04.02.2002 // The following agent loops through all unprocessed (selected) documents, and zips all // attachments unless they have the ending *.zip. // To mark the processed documents, the agent adds in the subject field the text // "<contains zipped files at the end>" //////////////////////////////////////////////////////////////////////////////// /////////
// used classes import lotus.domino.*; import java.io.*; import java.util.*; import java.util.zip.*; import java.util.Vector; import java.util.Enumeration;
// standard notes class public class JavaAgent extends AgentBase {
// main programm public void NotesMain() { try { // declaration of some variables FileOutputStream fos; RichTextItem rtitem; String attachmentname; String archivename; String oldarchive; byte b[] = new byte[512]; // initialize Session session = getSession(); AgentContext agentContext = session.getAgentContext(); Database db = agentContext.getCurrentDatabase(); DocumentCollection col = agentContext.getUnprocessedDocuments(); Document doc = col.getFirstDocument(); // loop through all documents while (doc != null) { // save flag boolean saveFlag = false; // location of the data dir archivename = session.getEnvironmentString("Directory",true) + "\\" + doc.getNoteID() + ".zip"; // create an new file and zip output stream fos = new FileOutputStream(archivename); ZipOutputStream zout = new ZipOutputStream(fos); // get the bodyfield rtitem = (RichTextItem) doc.getFirstItem("Body"); // get all embedded objects and the number Vector v = rtitem.getEmbeddedObjects(); Enumeration e = v.elements(); // loop through all objects while (e.hasMoreElements()) { EmbeddedObject eo = (EmbeddedObject)e.nextElement(); // check if it's an attachment if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT) { // check the ending of the attachment, if it's a zip file, then it doesn't make sense to zip it again if ( eo.getName().toUpperCase().endsWith( ".ZIP" )); else { InputStream in = eo.getInputStream(); ZipEntry ze = new ZipEntry(eo.getName().replace(File.separatorChar,'/')); zout.putNextEntry(ze); int len=0;
// read the bytes while((len=in.read(b)) != -1) { zout.write(b,0,len); } // close the entry and remove the attachment zout.closeEntry(); eo.remove(); saveFlag = true; } } } // check if something has changed in the document if (saveFlag) { // close the file- and zip output stream zout.close(); fos.close(); // add some lines to the body item rtitem.addNewLine( 3 );
// attach the file rtitem.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null, archivename, archivename); // add some text to the subject line doc.replaceItemValue("Subject", doc.getItemValueString("Subject") + " <contains zipped files>"); // save the document doc.save(true, true); saveFlag = false; }
// close and delete the zip file fos.close(); File zipfile = new File(archivename); zipfile.delete(); // get the next document doc = col.getNextDocument(doc); } // handle the errors } catch (Exception e) { e.printStackTrace(); } } }