/* A file/directory Zipping/Unzipping utility @author: Mukul Gandhi Change log: Date Description ---- ----------- 17.02.2007 Created. Implemented zipping functionality. 22.03.2009 1) Cleaned up code 2) Added unzip support 3) The contents of zip file are now more like WinZip or WinRAR 23.03.2009 Allow user to select compression strength */ import java.util.zip.ZipOutputStream; import java.util.zip.ZipInputStream; import java.util.zip.Deflater; import java.util.zip.ZipEntry; import java.util.StringTokenizer; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.File; public class ZipUtil { private static ZipOutputStream zos = null; private static ZipInputStream zis = null; /* Main method of the class */ public static void main(String[] args) { if (args.length == 3) { if (!args[1].equals("-o")) { System.out.println("Error ...\n"); System.out.println("Usage (for unzipping): java ZipUtil -o \n"); System.exit(0); } else { // do unzip doUnzip(args[0], args[2]); } } else if (args.length == 4) { if (args[2].equals("-s") && (args[3].equals("d") || args[3].equals("b"))) { // do zipping operation doZip(args[0], args[1], args[3]); } else { System.out.println("Error ...\n"); System.out.println("Usage (for zipping): java ZipUtil [filename | dirname] -s [d | b]\n"); System.out.println("-s : to specify compression strength"); System.out.println("d - default compression, b - best compression"); System.exit(0); } } else { // wrong command-line System.out.println("Error ...\n"); System.out.println("Usage (for zipping): java ZipUtil [filename | dirname] -s [d | b]\n"); System.out.println("-s : to specify compression strength"); System.out.println("d - default compression, b - best compression\n"); System.out.println("OR\n"); System.out.println("Usage (for unzipping): java ZipUtil -o \n"); System.exit(0); } } // end of method, 'main' /* Method to zip/compress a list of files */ private static void doZip(String file, String outFile, String strength) { try { File input = new File(file); zos = new ZipOutputStream(new FileOutputStream(outFile)); if (strength.equals("d")) { zos.setLevel(Deflater.DEFAULT_COMPRESSION); } else if (strength.equals("b")) { zos.setLevel(Deflater.BEST_COMPRESSION); } if (input.isFile()) { // if input is a single file System.out.println("adding: "+input.getCanonicalPath()); String canPath = input.getCanonicalPath(); String entryName = canPath.substring(canPath.lastIndexOf('\\') + 1); zos.putNextEntry(new ZipEntry(entryName)); FileInputStream fis = new FileInputStream(input); byte[] buf = new byte[1024 * 100]; // 100 KB buffer int bytesRead; while ((bytesRead = fis.read(buf)) != -1) { zos.write(buf, 0, bytesRead); } fis.close(); zos.closeEntry(); } else { // if input is a directory File[] dirList = input.listFiles(); zipDir(input.getCanonicalPath(), dirList); } zos.close(); } catch (Exception ex) { ex.printStackTrace(); } } // end of method, 'doZip' /* Method to compress a list of files (a directory). called by method, doZip. */ private static void zipDir(String dirPath, File[] dirList) { try { String dirPrefix = dirPath.substring(dirPath.lastIndexOf("\\") + 1); for (int i = 0; i < dirList.length; i++) { if (dirList[i].isFile()) { System.out.println("adding: "+dirList[i].getCanonicalPath()); String canPath = dirList[i].getCanonicalPath(); String entryName = dirPrefix + canPath.substring(dirPath.length()); zos.putNextEntry(new ZipEntry(entryName)); FileInputStream fis = new FileInputStream(dirList[i]); byte[] buf = new byte[1024 * 100]; // 100 KB buffer int bytesRead; while ((bytesRead = fis.read(buf)) != -1) { zos.write(buf, 0, bytesRead); } fis.close(); zos.closeEntry(); } else { // process subdirectory zipDir(dirPath, dirList[i].listFiles()); } } } catch (Exception ex) { ex.printStackTrace(); } } // end of method, 'zipDir' /* Method to unzip/decompress a zip archive */ private static void doUnzip(String inputFile, String outDir) { try { zis = new ZipInputStream(new FileInputStream(inputFile)); ZipEntry zipentry = null; while ((zipentry = zis.getNextEntry()) != null) { String entryName = zipentry.getName(); String filePath = outDir + "\\" + entryName; createMissingDirs(filePath.substring(0, filePath.lastIndexOf("\\"))); System.out.println("extracting to: "+filePath); FileOutputStream fos = new FileOutputStream(filePath); byte[] buf = new byte[1024 * 100]; // 100 KB buffer int bytesRead; while ((bytesRead = zis.read(buf, 0, buf.length)) != -1) { fos.write(buf, 0, bytesRead); fos.flush(); } fos.close(); zis.closeEntry(); } } catch (Exception ex) { ex.printStackTrace(); } } // end of method, 'doUnzip' /* Helper function to create missing directories. called by method, doUnzip. */ private static void createMissingDirs(String dirPath) { String tempPath = ""; StringTokenizer tokenizer = new StringTokenizer(dirPath, "\\"); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (tempPath.equals("")) { tempPath = token; } else { tempPath = tempPath + "\\" + token; } File tempFile = new File(tempPath); if (!tempFile.exists()) { tempFile.mkdir(); } } } // end of method, 'createMissingDirs' } // end of class