Tip

Zip files with Java LS2J script library

Jens Bruntt's tip Zip from LotusScript in Notes/Domino 6 shows you how to compress files to .zip format. It uses the ability of LotusScript to make calls to functionality that is available in the Java language using LS2J. You can also zip all files in a directory with a similar Java LS2J script library. This code shows you how.
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 ZipFiles {
// Note: 1st arg is the zipfile to produce
  public String zipMyFiles
(String zipFilePath, String
filesToZipFolder) {
    String result = "";

    try {
      ZipOutputStream zos = 
new ZipOutputStream(new 
FileOutputStream(zipFilePath));
      zos.setLevel
(Deflater.BEST_COMPRESSION); 
// compression style

      byte[] readBuffer = new byte[2156];
      int bytesin = 0;
      File filesFolder = new File
( filesToZipFolder ); 
// if filesFolder.isDirectory ???
      String[] fileList = filesFolder.list(); 
// flat directory, no hierarchy allowed

      for (int i = 0; i < fileList.length; i++) {
        File f =new File(filesFolder, fileList[i]);
        if (f.isDirectory()) {
        // ignore it
        }
        else {
          FileInputStream in = 
new FileInputStream(f);
                ZipEntry anEntry = 
new ZipEntry(f.getPath());
                zos.putNextEntry(anEntry);
                int len;
          while ((bytesin = 
in.read(readBuffer)) != -1) {
            zos.write(readBuffer, 0, bytesin);
          }
          zos.closeEntry();
          in.close();
        }
      }
      zos.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";
  }
}

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip exchange by member Mark Bryson. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

This was first published in September 2004

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.