sexta-feira, 21 de janeiro de 2011

Compactar arquivos com Notes 6

http://searchdomino.techtarget.com/tip/1,289483,sid4_gci1002690,00.html

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:tempdocument.doc"
varOutFilePath = "c:tempthezipfile.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";

}
}

Credit goes to this article that helped me get zipping to work: Zip meets Java from Devshed.

Extrair arquivos com Java

link: http://www.devshed.com/c/a/Java/Zip-Meets-Java/2/

So we can create zip files with Java. Can we extract them? Listing 2 shows an example Java class that can do just that. Let’s cover what’s going on in the class, as it might not be that obvious to the java.util.zip API newcomer.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipUncompressExample
{

   // specify buffer size for extraction
   static final int BUFFER = 2048;

   public static void main(String[] args)
   {
     try
     {
       System.out.println("Example of ZIP file decompression.");

       // Specify file to decompress
       String inFileName = "c:\example.zip";
       // Specify destination where file will be unzipped
       String destinationDirectory = "c:\temp\";

       File sourceZipFile = new File(inFileName);
       File unzipDestinationDirectory = new File(destinationDirectory);

       // Open Zip file for reading
       ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

       // Create an enumeration of the entries in the zip file
       Enumeration zipFileEntries = zipFile.entries();

       // Process each entry
       while (zipFileEntries.hasMoreElements())
       {
         // grab a zip file entry
         ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

         String currentEntry = entry.getName();
         System.out.println("Extracting: " + entry);

         File destFile =
           new File(unzipDestinationDirectory, currentEntry);

         // grab file's parent directory structure
         File destinationParent = destFile.getParentFile();

         // create the parent directory structure if needed
         destinationParent.mkdirs();

         // extract file if not a directory
         if (!entry.isDirectory())
         {
           BufferedInputStream is =
             new BufferedInputStream(zipFile.getInputStream(entry));
           int currentByte;
           // establish buffer for writing file
           byte data[] = new byte[BUFFER];

           // write the current file to disk
           FileOutputStream fos = new FileOutputStream(destFile);
           BufferedOutputStream dest =
           new BufferedOutputStream(fos, BUFFER);

           // read and write until last byte is encountered
           while ((currentByte = is.read(data, 0, BUFFER)) != -1)
           {
             dest.write(data, 0, currentByte);
           }
           dest.flush();
           dest.close();
           is.close();
         }
       }
       zipFile.close();
     }
     catch (IOException ioe)
     {
     ioe.printStackTrace();
     }
   }
}

The ZipUncompressExample class unzips a file called example.zip and extracts it to a destination directory called d:temp. We first open the source zip file. From our ZipFile object, we can use the entires() method to obtain an Enumeration which contains ZipEntry objects. These individual ZipEntry objects represent the entries which make up the Zip file.

I then use a while loop to cycle through the entries of our Zip file. As we process each ZipEntry object, we have to check and see if the zip entry in question has the needed housing directory structure in place. If the needed directories are not created, they are established. We use the getParentFile method of the File class to obtain the parent directory structure. If the current zip entry is not a directory (i.e., a file), we write the file to disk in the corresponding housing directory. This process is continued until each entry in the Zip file is processed.

Copiar campos RichText para de um documento para outro

http://moorthidaniel.com/lotus-script/copy-richtextitem-from-one-document-to-another-document/
 
%REM
Function updateRTitem
Description: Copy RichTextItem from One document to Another document
newdoc - destination document
refdoc - Source document
rtName - RichText Field name to copy
%END REM
Function updateRTitem(newdoc As NotesDocument,refdoc As NotesDocument,rtName As String)
On Error GoTo handleError
Dim srcRTitem As NotesRichTextItem
Set srcRTitem = refdoc.Getfirstitem(rtName)
Dim nav As NotesRichTextNavigator