When you have a database which stores a lot of unzipped attachments, wouldn't it be nice if you could have an agent which periodically runs over them and zips them all? Sure could save a lot of space.
Well, you can and it's not even that difficult. The nice thing is you don't even need Winzip or any other tools to do it, it's already built into the Domino server and client! The magic word is "Java". Java has built in classes for zipping and unzipping files, which we are going to use for this small agent. There is just one small caveat: it only runs on R5.03 servers and above.
First, some things you need to make sure you have so as to get this example up and running:
Create a database, and in that database create a form. Call it whatever you want. On the form, create a rich text field called "Body" (you can call it something else, but then you'll have to modify the agent code below). Also create a view that shows these documents. Now create an agent which should run manually from the actions menu on selected documents.
Copy and paste the code below in to the agent. Don't get scared if it doesn't make a whole lot of sense. There are plenty of comments in the .Java file you can download here - ZipUp.java
// Written by Erwin van Hunen
//
// In this example the agent is run manually from the actions menu on selected documents,
// but of course it would make much more sense to run it as a scheduled or triggered agent
// in the background
import lotus.domino.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
FileOutputStream fos;
Item item;
RichTextItem rtitem;
String attachmentname;
Vector items;
String archivename;
byte b[] = new byte[512];
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database db = agentContext.getCurrentDatabase();
DocumentCollection col = agentContext.getUnprocessedDocuments();
Document doc = col.getFirstDocument();
while (doc != null)
{
archivename = session.getEnvironmentString("Directory",true) + "\\" + doc.getNoteID() + ".zip";
fos = new FileOutputStream(archivename);
ZipOutputStream zout = new ZipOutputStream(fos);
rtitem = (RichTextItem) doc.getFirstItem("Body");
items = doc.getItems();
for (int i=0; i<items.size(); i++) {
item = (Item) items.elementAt(i);
if(item.getType() == Item.ATTACHMENT)
{
if (item.getValueString().length() != 0)
{
attachmentname = item.getValueString();
EmbeddedObject attachment = (EmbeddedObject) doc.getAttachment(attachmentname);
InputStream in = attachment.getInputStream();
ZipEntry e = new ZipEntry(attachment.getName().replace(File.separatorChar,'/'));
zout.putNextEntry(e);
int len=0;
while((len=in.read(b)) != -1)
{
zout.write(b,0,len);
}
zout.closeEntry();
attachment.remove();
}
}
}
zout.close();
fos.close();
doc.replaceItemValue("Body","");
rtitem.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null, archivename, archivename);
doc.save(true);
File zipfile = new File(archivename);
zipfile.delete();
doc = col.getNextDocument(doc);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Copyright © 2000 - 2024 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net