segunda-feira, 14 de maio de 2012

Zipar um arquivo no Notes

No exemplo abaixo, foi criado um agente chamando uma  biblioteca de scripts java:

AGENTE:
######################################################################
(Options)

Uselsx "*javacon" 'Which lets you use Java from LotusScript
Use "ZipFile" 'A Java library that holds a function to do zipping



Sub Zip()
Dim js As JAVASESSION
Dim zipClass As JAVACLASS
Dim zipFileObject As JavaObject
Dim strOutFilePath As String, strReturnCode As String

Set js = New JAVASESSION
Set zipClass = js.GetClass("ZipFile")
Set zipFileObject = zipClass.CreateObject
strOutFilePath = Strleft(StrArquivo, ".") + ".zip"

strReturnCode = zipFileObject.zipMyFile(StrArquivo, strOutFilePath, 1) 'Calling the zip function

Kill StrArquivo
StrArquivo = strOutFilePath

If Not strReturnCode = "OK" Then Error 1010, "An Error occurred"

End Sub
######################################################################


Código da biblioteca de scripts:
######################################################################

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, int intType) {
  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);

if (intType == 1)
{
fileToZip  = fileToZip.substring(fileToZip.lastIndexOf("\\"), fileToZip.length());
}

          // 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";

  }
}

######################################################################




Nenhum comentário:

Postar um comentário