OpenNTF.org - Zip File and Reattach it
    Advanced
   OpenNTF Code Bin
Edit Document Code By Date > Code Document
About This Code
Brief Description:
Zip File and Reattach it 
Rating:
Not Rated Yet 
Contributor:
Peter Finken 
Category:
Application 
Type:
Example Code 
Document Release:
1.0 
Notes Version:
R6.x, R7.x 
Last Modified:
14 Jun 2007 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
This example detach a file from a NotesRichtextItem, zip this file & reattach it to the NotesRichtItem
Usage / Example
Code Attachments
zip.ntf (512 Kbytes)
7z442.exe (817 Kbytes)
 Comments
Posted by Andre Guirard on 06/27/2007 10:56:44 AMPlease explain a little further...
What is the EXE file for, and why should we run it?
Posted by Peter Finken on 07/04/2007 06:37:47 AMexe-file
The exe-file will compress the attachments. It's Open Source-Tool called 7zip.
That only runs on Windowssystems, i will add a zip-sample which is implemented in Java in the next days
Posted by Matt Quelch on 10/01/2007 06:31:39 AMWhy not use Java?
I've implemented a similar function using a LotusScript Agent and a Java Script Library (based on Jens Bruntt's article: http://searchdomino.techtarget.com/tip/1,289483,sid4_gci1002690,00.html):
ZipFile.java:
import java.io.File;
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 fileName, String zipFileName) {
String result = "";
byte[] buffer = new byte[18024];
// Get the basename from fileName
String baseName = (new File(fileName)).getName();
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
// Set the compression ratio
out.setLevel(Deflater.BEST_COMPRESSION);
System.out.println(fileName);
// Associate a file input stream for the current file
FileInputStream in = new FileInputStream(fileName);
// Add ZIP entry to output stream using baseName.
out.putNextEntry(new ZipEntry(baseName));
// Transfer bytes from the current file to the ZIP file
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
// Close the entries and streams
out.closeEntry();
in.close();
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";
}
}
Agent:
Option Public
Option Declare
Uselsx "*javacon"
Use "ZipFile"
Sub Initialize
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim srcdoc As NotesDocument
Dim modified As Boolean
Dim report As Variant
Dim dirTemp As Variant
Dim js As JAVASESSION
Dim zipClass As JAVACLASS
Dim zipFileObject As JavaObject
Dim varFileToZip As String, varOutFilePath As String, returnCode As String
Set uidoc = ws.CurrentDocument
Set srcdoc = uidoc.Document
Set js = New JAVASESSION
Set zipClass = js.GetClass("ZipFile")
Set zipFileObject = zipClass.CreateObject
dirTemp = Environ("Temp")
Set report = srcdoc.GetFirstItem("Report")
If report.Type = RICHTEXT Then
' Zip each attachment
Forall attach In report.EmbeddedObjects
If attach.Type = EMBED_ATTACHMENT Then
varFileToZip = dirTemp + "\" + attach.Name
varOutFilePath = dirTemp + "\" + attach.Name + ".zip"
Print "Exporting..."
Call attach.ExtractFile(varFileToZip)
' Zip attachment
Print "Compressing..."
returnCode = zipFileObject.zipMyFile(varFileToZip, varOutFilePath)
If Not returnCode = "OK" Then
Print "An Error occurred"
' Remove attachment and attach zipped version
Else
Print "Importing..."
Call attach.Remove
Call report.EmbedObject(1454, "", varOutFilePath)
modified = True
Print "Done."
End If
End If
End Forall
If modified Then Call srcdoc.Save(True, False, True)
Call uidoc.Close
End If
End Sub
Posted by cool dude on 11/29/2007 10:51:16 AMword 2003
This code works great.
Only issue I have is that we are currently running word 2000 and when someone sends a 2003 word file the printer goes mad with pages and pages of rubbish.
Is there a way to alter the code to error on a 2003 word document?
Posted by Martin Michaelis on 12/07/2007 06:39:40 AMObject variable not set
When i start the agent, i get a "Object variable not set"-error!
First i thought it's the TEMP variable in the notes.ini, but after setting it the error remains!
Any suggestions?
 Add your comment!