Shrink Lotus Notes databases with many attachments

Discover how you can trim the size of Lotus Notes databases that have large amounts of attachments with this Java code.

Use this Java code to reduce the size of Lotus Notes databases with large amounts of attachments.
Related resources from SearchDomino.com:
Tip: Reduce the size of your Lotus Notes database

Expert Advice: Calculating the size of a Lotus Notes database

FAQ: Java for Lotus Notes and Domino

Java for Lotus Notes Domino Reference Center

Suppose that there 
is a form which has 
a RichText called Body.
We must make a 
Script Library called 
ZipHelper whit next code:
//// Begin ZipHelper code
import java.io.*; 
import java.util.*;
import java.util.zip.*;

public class ZipHelper
{
final int BUFFER = 2048;

public String getTempDirectory()
{
return System.getProperty("java.io.tmpdir");
}
 
public String zipMultipleFiles(Vector 
filesToZip, String zipFilePath) 
{
String result = ""; 

byte[] buffer = 
new byte[18024];
  
// Specify zip file name
String zipFileName = zipFilePath;
try
{
ZipOutputStream out = new ZipOutputStream
(new FileOutputStream(zipFileName));
 
 // Set the compression ratio
out.setLevel(Deflater.BEST_COMPRESSION);
Enumeration e = filesToZip.elements();
while (e.hasMoreElements())
{
String fileToZip = (String)e.nextElement();
if((new File(fileToZip)).exists())
{
System.out.println(fileToZip);
// Associate a file input stream for the current file 
FileInputStream in = new FileInputStream(fileToZip);
   
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileToZip));
 
  // Transfer bytes from the current file to the ZIP file
 //out.write(buffer, 0, in.read(buffer));
 
   int len;
while ((len = in.read(buffer)) > 0) 
        {
out.write(buffer, 0, len);
}
          
  // Close the current entry
out.closeEntry();

 // Close the current file input stream
in.close();
       }
           }

  // Close the ZipOutPutStream
out.close();
   } 
catch (IllegalArgumentException iae)
{
iae.printStackTrace();
return "ERROR_ILLEGALARGUMENTSEXCEPTION";
 }
   catch (FileNotFoundException fnfe) 
  {
fnfe.printStackTrace();
return "ERROR_FILENOTFOUND";
   }
 catch (IOException ioe)
    {
 ioe.printStackTrace();
return "ERROR_IOEXCEPTION";
  }
 return "OK";
 }
 
public String zipMyFile(String fileToZip, String zipFilePath) 
 {
String result = ""; 

byte[] buffer = new byte[18024];

// Specify zip file name
String zipFileName = zipFilePath;
try
{
ZipOutputStream out = new ZipOutputStream
(new FileOutputStream(zipFileName));
   
 // Set the compression ratio
out.setLevel(Deflater.BEST_COMPRESSION);

System.out.println(fileToZip);
// Associate a file input stream for the current file 
FileInputStream in = new FileInputStream(fileToZip);
 
  // Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileToZip));
 
 // Transfer bytes from the current file to the ZIP file
  //out.write(buffer, 0, in.read(buffer));
 
 int len;
  while ((len = in.read(buffer)) > 0) 
           {
out.write(buffer, 0, len);
           }
          
  // Close the current entry
out.closeEntry();
 
    // Close the current file input stream
  in.close();
 
 // Close the ZipOutPutStream
out.close();
       } 
 catch (IllegalArgumentException iae)
{
iae.printStackTrace();
return "ERROR_ILLEGALARGUMENTSEXCEPTION";
      }
   catch (FileNotFoundException fnfe) 
{
fnfe.printStackTrace();
return "ERROR_FILENOTFOUND";
      }
    catch (IOException ioe)
       {
     ioe.printStackTrace();
 return "ERROR_IOEXCEPTION";
  }
    return "OK";
 } 
 
public String unzipMyFile(String zipFilePath, 
String pathToUnzip) 
 {
try
{
zipFilePath = zipFilePath.replace('\', '/');
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream
( zipFilePath );   
ZipInputStream zis = new ZipInputStream
(new BufferedInputStream(fis));
ZipEntry entry;
int count;
byte data[] = new byte[BUFFER];
pathToUnzip = pathToUnzip.replace('\', '/');
if(pathToUnzip.charAt(pathToUnzip.length()-1) != '/')
pathToUnzip += "/";

while((entry = zis.getNextEntry()) != null) 
{
if( entry.isDirectory() ) //Directory
 {
   (new File(pathToUnzip + entry.getName())).mkdir();
     continue;
   }
  else //File
  {
  String newfile = pathToUnzip + entry.getName();
                   
   // write the files to the disk
   FileOutputStream fos = new FileOutputStream
(pathToUnzip + 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();
}
catch (IllegalArgumentException iae)
{
iae.printStackTrace();
return "ERROR_ILLEGALARGUMENTSEXCEPTION";
       }
    catch (FileNotFoundException fnfe) 
  {
fnfe.printStackTrace();
return "ERROR_FILENOTFOUND";
       }
    catch (IOException ioe)
       {
     ioe.printStackTrace();
 return "ERROR_IOEXCEPTION";
       }
catch(Exception e) 
  {
e.printStackTrace();
return "UNKNOWN_ERROR";
  }
return "OK";
 }
}
//// End ZipHelper code

Next we must create a Java agent 
called ZipAttachments triggered 
On event "Agent List Selection" whith Next Code

//// Begin ZipAttachments agent code
import lotus.domino.*;
import java.io.*;
import java.util.Vector;
import java.util.Enumeration;

public class JavaAgent extends 
AgentBase {

public void NotesMain() {

try {
ZipHelper zipHelper = new ZipHelper();
Session session = getSession();
String tempDir = System.getProperty
("java.io.tmpdir");
String zipFile = new String
( "Attachments.zip" );
AgentContext agentContext = 
session.getAgentContext();

Database db = agentContext.
getCurrentDatabase();
Document doc = agentContext.
getDocumentContext();
RichTextItem body = (RichTextItem)
doc.getFirstItem("Body");
Vector v = body.getEmbeddedObjects();
Vector vNames = new Vector();
Enumeration e = v.elements();
while (e.hasMoreElements())
 {
EmbeddedObject eo = 
(EmbeddedObject)e.nextElement();
if (eo.getType() == 
EmbeddedObject.EMBED_ATTACHMENT)
 {
eo.extractFile( tempDir + eo.getSource() );
vNames.add( tempDir + eo.getSource() );
eo.remove();
doc.save(true, false);
}
 }
zipHelper.zipMultipleFiles(vNames, 
tempDir + zipFile);
if((new File(tempDir +  zipFile)).exists())
 {
body.embedObject(EmbeddedObject.
EMBED_ATTACHMENT, null, tempDir 
+  zipFile,  zipFile);
(new File(tempDir + zipFile)).delete();
 }
doc.save(true, false);
e = vNames.elements();
while(e.hasMoreElements())
{
String fileName = (String)e.nextElement();
if((new File(fileName)).exists())
 {
(new File(fileName)).delete();
}
 }
} catch(Exception e) {
e.printStackTrace();
  }
 }
}
//// End ZipAttachments agent code

Note: Before saving, we must click "Edit Project" and add: (Library)ZipHelper.

In the form PostSave event we must enter:

@Command
([ToolsRunMacro]; 
"(ZipAttachments)" );
@Command([CloseWindow])

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip library by member Jairo Villanueva. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.

This was first published in October 2007

Dig deeper on Java for Lotus Notes Domino

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchDataCenter

SearchExchange

SearchContentManagement

Close