I’ve found a bit of free code for zipping files in Java with basic instr. What I don’t understand is how to get it to work as I had hoped. If anyone out there would be kind enough to help that would be great!
The idea is to call a java agent from lotus script and pass the folder I want zipped (there is plenty of documentation on this, i first want to get it to work with a hard coded folder as in the below obviously incorrect code).
This is the source for the Java library.
FYI: The (Use “ZIP”) is the name of the java libray below & for security reasons the %folder% represents the entire path including drive letter. Most of this code is the
// Command Line:
// java ZipBackup [-options] []
//
// Parameters:
//
// options (Optional):
// r = Recursive folders (default)
// R = do not do Recursive folders
// l = enable Logging
// L = disable Logging (default)
// q = Quiet Mode (default)
// Q = non-Quiet Mode (displays progress to console)
// f = use Folder-script mode; uses folder as script
// F = normal script mode (not Folder Mode) (default)
// 0-9 = Level of compression to use; 0 - no compression; 9 - maximum compression (default)
//
// archive-folder:
// location to store Zip files generated by ZipBackup; typically I put all backup zip files in a “c:\archive” folder.
//
// script:
// Specify the Text file containing instructions for performing file backups. Right now, the format is simple: one line containing the path to each folder you want backed-up.
//
// suffix:
// Optional. To help organize zip files containing specific content, you may append a file “suffix”.
//
// %EndRem
//
// '*** Class Code Below ***
import java.util.*;
import java.io.*;
import java.util.zip.*;
import java.text.*;
public class ZipBackup
{
String zipFileName = null;
String logFileName = null;
Map archiveQue = null;
LogFile log = null;
String archiveLocation = null;
String fileSeparator = null;
// options
boolean doFolderMode = false;
boolean doLogging = false;
boolean doRecursion = true;
boolean doQuietMode = true;
int compression = 9;
int recursionLevel = 0;
public ZipBackup(String[] args)
{
boolean useOptions = false;
if (args.length > 0)
{
if (args[0].trim().startsWith("-"))
{
useOptions = true;
parseOptions(args[0].trim());
}
}
if ((useOptions && args.length < 3) || (!useOptions && args.length < 2))
{
// invalid number of arguments, so display usage message
printUsageMessage();
return;
}
String scriptFileName = null;
String fileNameSuffix = null;
if (useOptions)
{
if (args.length >= 3)
{
archiveLocation = args[1].trim();
scriptFileName = args[2].trim();
}
if (args.length >= 4)
{
fileNameSuffix = args[3].trim();
}
}
else
{
if (args.length >= 2)
{
archiveLocation = args[0].trim();
scriptFileName = args[1].trim();
}
if (args.length >= 3)
{
fileNameSuffix = args[2].trim();
}
}
generateZipFileName(archiveLocation, fileNameSuffix);
if (doLogging)
{
log = new LogFile(logFileName, !doQuietMode);
log.println("Zip File Name: " + zipFileName);
}
if (!doLogging && !doQuietMode)
System.out.println("Zip File Name: " + zipFileName);
List scriptEntrys = null;
if (doFolderMode)
{
scriptEntrys = new ArrayList();
scriptEntrys.add(scriptFileName);
}
else
{
scriptEntrys = parseArchiveScript(scriptFileName);
if (scriptEntrys == null)
{
printUsageMessage();
log.close();
return;
}
}
try
{
setupArchiveMap(scriptEntrys);
if (doLogging)
{
log.println("Start Creating Zip");
}
else
{
if (!doQuietMode)
System.out.println("Start Creating Zip");
}
create();
if (doLogging)
{
log.println("Finished Creating Zip");
}
else
{
if (!doQuietMode)
System.out.println("Finished Creating Zip");
}
}
catch (Exception e)
{
if (doLogging)
log.printException(e);
else
e.printStackTrace();
}
finally
{
if (doLogging)
log.close();
}
}
private void setupArchiveMap(List archiveFolders)
{
archiveQue = new HashMap();
File myFolder = null;
for (Iterator it = archiveFolders.iterator(); it.hasNext(); )
{
myFolder = new File((String) it.next());
if (myFolder.exists())
{
recursionLevel = 0;
addToArchiveQue(myFolder);
}
}
}
private void addToArchiveQue(File file)
{
recursionLevel++;
if (file.isDirectory())
{
if (doRecursion || recursionLevel < 2)
{
File[] files = file.listFiles();
List fileList = Arrays.asList(files);
for (Iterator it = fileList.iterator(); it.hasNext(); )
{
addToArchiveQue((File) it.next());
}
}
}
else
{
String fileName = file.getAbsolutePath();
String entryName = convertToEntryName(fileName);
archiveQue.put(entryName, fileName);
}
}
private String convertToEntryName(String filename)
{
int dirColon = filename.indexOf(":");
if (dirColon != -1)
{
filename = filename.substring(dirColon + 2);
}
filename = filename.replace('\\', '/');
return filename;
}
private void generateZipFileName(String location, String suffix)
{
String fileSeparator = System.getProperty("file.separator");
Calendar cal = Calendar.getInstance(); // gets current date/time
int year = cal.get(Calendar.YEAR);
int month = cal.get (Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get (Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
NumberFormat format = NumberFormat.getInstance();
format.setMinimumIntegerDigits(2);
format.setMaximumIntegerDigits(2);
format.setMinimumFractionDigits(0);
format.setMaximumFractionDigits(0);
StringBuffer filename = new StringBuffer();
filename.append(location);
// if the directory location does not end with the file separator,
// then append it before the file name.
if (!location.endsWith(fileSeparator))
{
filename.append(fileSeparator);
}
filename.append("archive-");
filename.append(year);
filename.append("-");
filename.append(format.format(month + 1));
filename.append("-");
filename.append(format.format(day));
filename.append("_");
filename.append(format.format(hour));
filename.append("-");
filename.append(format.format(minute));
filename.append("-");
filename.append(format.format(second));
if (suffix != null)
{
filename.append("-");
filename.append(suffix);
}
zipFileName = filename.toString() + ".zip";
logFileName = filename.toString() + ".log";
}
private List parseArchiveScript(String fileName)
{
File myFile = new File(fileName);
File testFile = null;
if (!myFile.exists() || myFile.isDirectory())
return null;
FileReader fileReader = null;
try
{
fileReader = new FileReader(myFile);
BufferedReader read = new BufferedReader(fileReader);
List myList = new ArrayList();
String line = null;
while ((line = read.readLine()) != null)
{
line = line.trim();
if (line.length() > 0)
{
testFile = new File(line);
if (testFile.exists())
myList.add(line);
}
}
return myList;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
try
{
fileReader.close();
}
catch (IOException ioe)
{
log.printException(ioe);
}
}
}
private void create()
throws IOException
{
ZipOutputStream zipStream =
new ZipOutputStream( new FileOutputStream(zipFileName));
zipStream.setLevel(compression);
Map.Entry entry = null;
String currentFileName = null;
String currentEntryName = null;
FileInputStream fis = null;
byte[] buffer = new byte[ 10240 ];
int readLength = 0;
int fileCount = archiveQue.size();
int count = 0;
double percent = 0.0;
NumberFormat format = NumberFormat.getPercentInstance();
format.setMinimumFractionDigits(0);
format.setMaximumFractionDigits(1);
format.setMaximumIntegerDigits(3);
format.setMinimumIntegerDigits(1);
for (Iterator it = archiveQue.entrySet().iterator(); it.hasNext(); )
{
entry = (Map.Entry) it.next();
currentEntryName = (String) entry.getKey();
currentFileName = (String) entry.getValue();
count++;
zipStream.putNextEntry( new ZipEntry(currentEntryName) );
fis = new FileInputStream(currentFileName);
if (doLogging)
{
log.print(currentFileName);
percent = (double) count / fileCount;
log.println(" (" + count + " of " + fileCount + "; " + format.format(percent) + ")");
}
for (int i = 0; ; i++)
{
readLength = fis.read(buffer);
if (readLength < 0)
{
if (doLogging)
log.println("=" + i);
break;
}
zipStream.write(buffer, 0, readLength);
if (doLogging)
{
if ((i % 10) == 0)
log.print("*");
}
}
fis.close();
zipStream.closeEntry();
if (doLogging)
log.println();/*
public PrintWriter getPrintWriter()
{
return this.logPrintWriter;
}
*/
if (!doQuietMode && !doLogging)
{
if ((count % 10) == 0)
System.out.print("*");
}
}
zipStream.close();
if (!doQuietMode && !doLogging)
{
System.out.println("Archived " + count + " of " + fileCount);
}
if (doLogging)
{
log.println();
log.println("Archived " + count + " of " + fileCount);
}
}
private void parseOptions(String options)
{
if (options.indexOf("Q") != -1)
this.doQuietMode = false;
if (options.indexOf("q") != -1)
this.doQuietMode = true;
if (options.indexOf("L") != -1)
this.doLogging = false;
if (options.indexOf("l") != -1)
this.doLogging = true;
if (options.indexOf("R") != -1)
this.doRecursion = false;
if (options.indexOf("r") != -1)
this.doRecursion = true;
if (options.indexOf("F") != -1)
this.doFolderMode = false;
if (options.indexOf("f") != -1)
this.doFolderMode = true;
if (options.indexOf("0") != -1)
this.compression = 0;
else if (options.indexOf("1") != -1)
this.compression = 1;
else if (options.indexOf("2") != -1)
this.compression = 2;
else if (options.indexOf("3") != -1)
this.compression = 3;
else if (options.indexOf("4") != -1)
this.compression = 4;
else if (options.indexOf("5") != -1)
this.compression = 5;
else if (options.indexOf("6") != -1)
this.compression = 6;
else if (options.indexOf("7") != -1)
this.compression = 7;
else if (options.indexOf("8") != -1)
this.compression = 8;
else if (options.indexOf("9") != -1)
this.compression = 9;
}
private void printUsageMessage()
{
StringBuffer sb = new StringBuffer();
sb.append("Usage: \n");
sb.append("\tjava com.taylor.zip.ZipBackup [-<opts>] <archive> <script> [<suffix>]\n");
sb.append("\n\n");
sb.append("Parameters\n");
sb.append("==========\n");
sb.append("\topts:\t\tr = recursive folders (default)\n");
sb.append("\t\t\tR = no recursive folders\n");
sb.append("\t\t\tl = enable logging\n");
sb.append("\t\t\tL = disable logging (default)\n");
sb.append("\t\t\tq = enable quiet mode (default)\n");
sb.append("\t\t\tQ = disable quiet mode\n");
sb.append("\t\t\tf = folder is script\n");
sb.append("\t\t\tF = script text file (default)\n");
sb.append("\t\t\t0-9 = Compression level 0 (store only) to 9 (best). \n");
sb.append("\t\t\t 9 is default.\n\n");
sb.append("\tarchive:\tlocation (folder/directory) to create archive files\n");
sb.append("\tscript:\t\ttext script file (list of directories on each line)\n");
sb.append("\t\t\t or specific directory to backup (use with -f option)\n");
sb.append("\tsuffix:\t\tsuffix to append to end of archive file name\n");
System.out.println(sb.toString());
}
public static void main(String[] args) {
ZipBackup testProcessExec = new ZipBackup(args);
testProcessExec.invokedStandalone = true;
}
private boolean invokedStandalone = false;
private class LogFile
{
private String logFileName;
private boolean echoToSystemOut = false;
private PrintWriter logPrintWriter;
private boolean isActive = false;
public LogFile(String logFileName, boolean echoOn)
{
String dirSeparator = System.getProperty("file.separator");
String tempDir = System.getProperty("java.io.tmpdir");
this.echoToSystemOut = echoOn;
if (logFileName.indexOf(dirSeparator) > -1)
{
this.logFileName = logFileName;
}
else
{
this.logFileName = tempDir + dirSeparator + logFileName;
}
try
{
this.logPrintWriter =
new PrintWriter(new FileOutputStream(this.logFileName));
this.isActive = true;
}
catch (IOException e)
{
System.out.println("Problems creating/open " + this.logFileName);
}
}
public String getLogFileName()
{
return this.logFileName;
}
public boolean isActive()
{
return this.isActive;
}
public void close()
{
if (this.isActive)
{
this.logPrintWriter.close();
}
this.isActive = false;
}
public void println(String text)
{
if (this.isActive)
{
this.logPrintWriter.println(text);
if (this.echoToSystemOut)
System.out.println(text);
this.logPrintWriter.flush();
}
}
public void println(Object obj)
{
if (this.isActive)
{
this.logPrintWriter.println(obj);
if (this.echoToSystemOut)
System.out.println(obj);
this.logPrintWriter.flush();
}
}
public void println()
{
if (this.isActive)
{
this.logPrintWriter.println();
if (this.echoToSystemOut)
System.out.println();
this.logPrintWriter.flush();
}
}
public void print(String text)
{
if (this.isActive)
{
this.logPrintWriter.print(text);
if (this.echoToSystemOut)
System.out.print(text);
}
}
public void printList(List list)
{
if (this.isActive)
{
for (Iterator it = list.iterator(); it.hasNext(); )
{
println(it.next());
}
}
}
public void printMap(Map map)
{
if (this.isActive)
{
Map.Entry entry = null;
for (Iterator it = map.entrySet().iterator(); it.hasNext(); )
{
entry = (Map.Entry) it.next();
println(entry.getKey() + " = " + entry.getValue());
}
}
}
public void printException(Exception e)
{
if (this.isActive)
{
e.printStackTrace(this.logPrintWriter);
if (this.echoToSystemOut)
e.printStackTrace(System.out);
this.logPrintWriter.flush();
}
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append ("Log File: " + this.logFileName);
sb.append (" Active: " + this.isActive);
sb.append (" Echo to System.out: " + this.echoToSystemOut);
return sb.toString();
}
}
}
// **** This is the code I have in the agent ***
Use “ZIP”
Uselsx “*javacon”
Dim mySession As JavaSession
Dim myClass As JavaClass
Dim archive As JavaObject
Dim c As Integer
Set mySession = New JavaSession()
Set myClass = mySession.GetClass("ZipBackup")
Set archive = myClass.CreateObject()
c = archive.ZipBackup( "%folder%" )