001 /* 002 * ZipUtility.java 003 * 004 * Created on October 25, 2005, 2:38 PM 005 * 006 * This file is part of the STAR Scheduler. 007 * Copyright (c) 2003-2006 STAR Collaboration - Brookhaven National Laboratory 008 * 009 * STAR Scheduler is free software; you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation; either version 2 of the License, or 012 * (at your option) any later version. 013 * 014 * STAR Scheduler is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with STAR Scheduler; if not, write to the Free Software 021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 */ 023 024 package gov.bnl.star.offline.scheduler.util.sandbox; 025 026 /** 027 * Use to biuld ZIP packages. 028 * @author Levente Hajdu 029 */ 030 import java.io.File; 031 import java.io.FileOutputStream; 032 import java.util.zip.ZipEntry; 033 import java.util.zip.ZipOutputStream; 034 import java.io.*; 035 036 public class ZipUtility { 037 private ZipOutputStream cpZipOutputStream = null; 038 private String strSource = ""; 039 private String strTarget = ""; 040 private static long size = 0; 041 private static int numOfFiles = 0; 042 043 044 public static void main(String args[]) { 045 046 ZipUtility udZipUtility = new ZipUtility(); 047 udZipUtility.strSource = "C:\\LBHTemp\\*.*"; //args[0]; 048 udZipUtility.strTarget = "C:\\abc.zip"; //args[1]; 049 udZipUtility.zip(); 050 } 051 052 053 public static void Zipdir(String ScrDir, String zipFile) { 054 ZipUtility udZipUtility = new ZipUtility(); 055 udZipUtility.strSource = ScrDir; 056 udZipUtility.strTarget = zipFile; 057 udZipUtility.zip(); 058 059 } 060 061 062 063 private void zip(){ 064 try { 065 File cpFile = new File (strSource); 066 if (!cpFile.isFile() && !cpFile.isDirectory() ) { 067 System.out.println("\nSource file/directory Not Found!"); 068 return; 069 } 070 FileOutputStream fos = new FileOutputStream(strTarget); 071 cpZipOutputStream = new ZipOutputStream(fos); 072 cpZipOutputStream.setLevel(9); 073 zipFiles( cpFile); 074 cpZipOutputStream.finish(); 075 cpZipOutputStream.close(); 076 System.out.println("\n Finished creating zip file " + strTarget + " from source " + strSource); 077 System.out.println("\n Total of " + numOfFiles +" files are Zipped " ); 078 System.out.println("\n Total of " + size + " bytes are Zipped "); 079 } catch (Exception e){ 080 e.printStackTrace(); 081 } 082 } 083 084 085 086 private void zipFiles(File cpFile) { 087 088 int byteCount; 089 final int DATA_BLOCK_SIZE = 2048; 090 FileInputStream cpFileInputStream; 091 092 093 if (cpFile.isDirectory()) { 094 if(cpFile.getName().equalsIgnoreCase(".metadata")){ //if directory name is .metadata, skip it. 095 return; 096 } 097 File [] fList = cpFile.listFiles() ; 098 for (int i=0; i< fList.length; i++){ 099 zipFiles(fList[i]) ; 100 } 101 } 102 103 else { 104 try { 105 if(cpFile.getAbsolutePath().equalsIgnoreCase(strTarget)){ 106 return; 107 } 108 System.out.println("Zipping "+cpFile); 109 size += cpFile.length(); 110 //String strAbsPath = cpFile.getAbsolutePath(); 111 numOfFiles++; 112 String strAbsPath = cpFile.getPath(); 113 String strZipEntryName = strAbsPath.substring(strSource.length()+1, strAbsPath.length()); 114 115 116 //byte[] b = new byte[ (int)(cpFile.length()) ]; 117 118 cpFileInputStream = new FileInputStream (cpFile) ; 119 ZipEntry cpZipEntry = new ZipEntry(strZipEntryName); 120 cpZipOutputStream.putNextEntry(cpZipEntry ); 121 122 byte[] b = new byte[DATA_BLOCK_SIZE]; 123 while ( (byteCount = cpFileInputStream.read(b, 0, DATA_BLOCK_SIZE)) != -1) 124 { 125 cpZipOutputStream.write(b, 0, byteCount); 126 } 127 128 //cpZipOutputStream.write(b, 0, (int)cpFile.length()); 129 cpZipOutputStream.closeEntry() ; 130 } catch (Exception e) { 131 e.printStackTrace(); 132 } 133 } 134 } 135 }