How to Zip an attachment using Lotus Script

I have to accomplish the following tasks:

• Remove attachments from the Lotus Notes document and put them on the local drive;

• Zip the attachments;

• Put a password protection for each of the zip files;

• Re-attach the zip files back to the Lotus Notes document.

The one piece that I’ve really need a help with is – How to Zip attachments using the Lotus Script, meaning that it has to be done inside an agent. Also, how to put a password protection on the zip file using the Lotus Notes Script.

I would appreciate very much the response ASAP.

Subject: How to Zip an attachment using Lotus Script

This is from a tip that was on SearchDomino (I think) a while back. Look at the Java classes for zipping to determine if you can add a password to the zip file. Haven’t had to do it, so I don’t know.

LotusScript does not have a native class for compressing files to .zip format. In this tip, I’ll show you how change that, if you are running Notes or Domino R6 (or newer), by using the ability of LotusScript to make calls to functionality that is available in the Java language. This is done using LS2J – which is documented in Designer Help.

I have an agent that holds the following code in Initialize:

Sub Initialize

Dim js As JAVASESSION

Dim zipClass As JAVACLASS

Dim zipFileObject As JavaObject

Dim varFileToZip As String, varOutFilePath As String, returnCode As String

Set js = New JAVASESSION

Set zipClass = js.GetClass(“ZipFile”)

Set zipFileObject = zipClass.CreateObject

varFileToZip = “c:\temp\document.doc”

varOutFilePath = “c:\temp\thezipfile.zip”

returnCode = zipFileObject.zipMyFile(varFileToZip, varOutFilePath) 'Calling the zip function

If Not returnCode = “OK” Then

Print “An Error occurred”

Else

Print “Zip went OK”

End If

End Sub

In the Options of the agent I have the following:

Uselsx “*javacon” 'Which lets you use Java from LotusScript

Use “ZipFile” 'A Java library that holds a function to do zipping

Below is the Java Library that makes it possible to zip a file. You create a Java Library in the Designer in Shared code → Script Libraries, click New Java Library, remove the few lines of code that you get as a help for getting started, and paste the code below. Save and call the library “ZipFile.”

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.Deflater;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class ZipFile {

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”;

}

}

Subject: RE: How to Zip an attachment using Lotus Script

code works great.

Though a tiny flaw is, that it stores the complete path information of the source file.

I checked the API of java.util.zip, but didn´t find any clues where to say (store relative…or store no path informations)

Anybody knows where to set these ?

Thanks,

Julian

Subject: RE: How to Zip an attachment using Lotus Script

Thanks. I’ve tried it and it works!

But I’ve got to find a way of password protecting my zipped file. I’ve looked everywhere and I can’t find much.

Can you help?

Subject: RE: How to Zip an attachment using Lotus Script

Thank you for posting the code. I did find this one on the SearchDomino site and tried to run the agent. It didn’t however, zip the file. The “zipFileObject” was empty and so I’ve got the “returnCode” = “ERROR_FILENOTFOUND”. I think, something is missing, maybe some .jar files, not sure.Can somebody take a look or try to run the code to see the results ? Thanks.