Habe gerade mal ein wenig mit Java rumgespielt und einen Java-Agenten gewurschtelt, der alle Anhänge innerhalb der Datenbank zippt. Funktioniert bei mir unter N6, um das unter N5 zum laufen zu bringen ist eine kleine Änderung notwendig:
Statt v.get(...) ist dort elementAt(...) notwendig.
Freue mich auch über Feedback/Kritik...
Ach ja: Das ganze natürlich wie immer ohne Gewähr...
/* Jack The Zipper for Notes 6
17.9.2003
written by Marco Zehner - all rights reserved
Comments to
marco.zehner@gmx.de*/
import lotus.domino.*;
import java.util.Vector;
import java.util.Enumeration;
import java.util.zip.*;
import java.io.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database db;
db = agentContext.getCurrentDatabase();
DocumentCollection col;
Document doc;
RichTextItem body;
EmbeddedObject attachement, zippedAttachement;
String attachementName;
String zipArgs[]=new String[1];
String ext;
final String PATH_EXT="c:\\extracts\\";
Zipper zippy;
zippy=new Zipper();
col=db.getAllDocuments();
int i=0, c=0;
for (i=1;i<col.getCount()+1;i++)
{
doc=col.getNthDocument(i);
body=(RichTextItem) doc.getFirstItem("Body");
if (body!=null){
if (doc.hasEmbedded())
{
Vector v = body.getEmbeddedObjects();
c=0;
for(c=0;c<v.size();c++){
attachement=null;
attachement=(EmbeddedObject) v.get(c);
// if you have Notes 5.x use below:
//attachement=(EmbeddedObject) v.elementAt(c);
//Complete Zip Cycle
attachementName="";
attachementName=attachement.getSource();
ext=attachementName.substring(attachementName.length()-3).toLowerCase();
if (!(ext.equals( ".gz" ))) {
//Extract file
attachement.extractFile(PATH_EXT + attachementName);
//ZIP attachements
zipArgs[0]=PATH_EXT+attachementName;
zippy.zipp(zipArgs);
System.out.println("Zipped it");
//Append Zipped
body.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null, PATH_EXT+attachementName+".gz",attachementName+".gz");
//delete unzipped in document
attachement.remove();
//save document
doc.save(true, false, true);
//delete files
//Sourcefile
File f = new File(PATH_EXT+attachementName);
if (!f.exists())
{System.out.println("Loeschen: Datei existiert nicht: " + attachementName);}
else
{ if (!f.canWrite())
{System.out.println("Loeschen: schreibgeschuetzt: "+ attachementName);}
else
{f.delete();
System.out.println("Gelöscht: "+ attachementName);}
}
//Zipped targetfile
File ff = new File(PATH_EXT+attachementName+".gz");
if (!ff.exists())
{System.out.println("Loeschen: Datei existiert nicht: " + attachementName);}
else
{ if (!ff.canWrite())
{System.out.println("Loeschen: schreibgeschuetzt: "+ attachementName);}
else
{ff.delete();
System.out.println("Gelöscht: "+ attachementName+".gz");}
}
}
}
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Dann weitere Klasse anlegen:
import java.util.zip.*;
import java.io.*;
public class Zipper {
private static int BLOCKSIZE = 8192;
public static void zipp(String[] args) {
String arg=args[0];
if ( arg== "" ) {
return;
}
try
{
GZIPOutputStream zipout =
new GZIPOutputStream( new FileOutputStream(arg + ".gz") );
byte buffer[] = new byte[BLOCKSIZE];
FileInputStream in = new FileInputStream( arg );
for ( int length; (length = in.read(buffer, 0, BLOCKSIZE)) != -1; )
zipout.write( buffer, 0, length );
in.close();
zipout.close();}
catch ( IOException e )
{
System.out.println( "Error: Couldn't compress "+arg );
}
}
}