/* Submit : A Course Project Submission Program * * Copyright (C) 1998 Alexander V. Konstantinou (akonstan@acm.org) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * $Id: ZipFileUtils.java,v 1.1 1998/11/19 19:50:16 akonstan Exp $ */ package org.acm.akonstan.util; import java.io.*; import java.util.Stack; import java.util.Vector; import java.util.Enumeration; import java.util.zip.*; public class ZipFileUtils { /** * Zip archives appear to use the '/' character as a separator independent * of the host file system convention. *
* @version $Revision: 1.1 $ ; $Date: 1998/11/19 19:50:16 $
* @author Alexander V. Konstantinou (akonstan@acm.org)
*/
public static final String zipSeparator = "/";
/**
* Zips recursively the contents of the source directory (srcDir) into
* the OutputStream out. If the verbose flag is set, the program
* will output the files added to System.out. The files are relative to
* the parent of the srcDir parameter.
*/
public static void CreateZipFile(File srcDir, OutputStream out,
boolean verbose)
throws IOException {
Vector fileVector = listRecursive(srcDir);
ZipOutputStream zout = new ZipOutputStream(out);
zout.setLevel(9);
zout.setComment("CreateZipFile Archive");
for(Enumeration en = fileVector.elements(); en.hasMoreElements(); ) {
String filename = (String) en.nextElement();
File file = new File(srcDir.getParent(), filename);
if (verbose) System.out.println(" adding: " + filename);
String zipFilename = filename;
// Convert filename to Zip separator format
if (File.separator.charAt(0) != zipSeparator.charAt(0)) {
zipFilename = filename.replace(File.separator.charAt(0), zipSeparator.charAt(0));
}
if (file.isFile()) {
ZipEntry ze = new ZipEntry(zipFilename);
zout.putNextEntry(ze);
FileInputStream fin = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fin);
byte[] buffer = new byte[4096];
int n = 0;
do {
n = bis.read(buffer, 0, 4096);
if (n != -1) zout.write(buffer, 0, n);
} while (n != -1);
fin.close();
} else {
/* ZipFile directory names must end in a zipSeparator */
ZipEntry ze = new ZipEntry(zipFilename + zipSeparator);
zout.putNextEntry(ze);
}
} // for (fileVector enumeration)
zout.close();
} // CreateZipFile
/**
* Returns a Vector of Strings containing a recursive listing of all
* files in the source directory. This list does not include the
* current directory or the parent directory, and is always relative
* to the parent directory.
*/
public static Vector listRecursive(File directory)
throws SecurityException, IOException {
Stack explore = new Stack();
Vector result = new Vector();
String root = directory.getParent();
// Schedule the directory for exploration
explore.push(directory.getName());
// While there are directories left to explore
while(!explore.empty()) {
String current = (String) explore.pop();
File curDir = new File(root, current);
String[] fileList = curDir.list();
if (fileList != null) {
for(int i=0; i