Tip

Shrink Lotus Notes databases with many attachments

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.