View Javadoc
1 /*** 2 * UnZip.java 3 * 4 * Project: Dependency Tool 5 * 6 * WHEN WHO WHAT 7 * 06.06.2003 pko initial public release 8 * 28.03.2002 ctr creation 9 * 10 * Copyright 2003 ELCA Informatique SA 11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland 12 * www.elca.ch 13 * 14 * This library is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU Lesser General Public License 16 * as published by the Free Software Foundation; either version 2.1 of 17 * the License, or (at your option) any later version. 18 * 19 * This library is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * Lesser General Public License for more details. 23 * 24 * You should have received a copy of the GNU Lesser General Public 25 * License along with this library; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 27 * USA 28 */ 29 30 package ch.elca.dependency.util; 31 32 import java.io.*; 33 import java.util.StringTokenizer; 34 import java.util.zip.ZipEntry; 35 import java.util.zip.ZipInputStream; 36 37 import org.apache.log4j.Logger; 38 39 /*** 40 * This helper class is used to extract jar archives into the specified 41 * directory. 42 * It creates the same relative directory structure in the specified output 43 * directory as it is in the jar archive. 44 * 45 * @author Christoph Trutmann 46 * @version 1.0-beta 47 */ 48 public class UnZip { 49 50 /*** 51 * Log4j Logger 52 */ 53 private static final Logger LOG = Logger.getLogger(UnZip.class); 54 55 /*** 56 * Data block size. 57 */ 58 protected final int DATA_BLOCK_SIZE = 2048; 59 60 /*** 61 * 62 * Extracts the files contained in the zipped file. 63 * 64 * <p><pre> 65 * The basic steps are: 66 * 1. get the zipped input stream 67 * 2. get the zipped entries and determine output location 68 * 3. prepare the uncompressed output stream 69 * 4. read source zipped data and write to uncompressed stream 70 * 5. close the source and target stream</pre> 71 * 72 * @param zipFile Abstract file object of the zip file. 73 * @param output Output location for the extracted files. 74 */ 75 public void extractFiles(File zipFile, File output) { 76 77 if (output.exists()) { 78 MyFile oldOutput = new MyFile(output.getPath()); 79 oldOutput.delete(); 80 } 81 82 FileInputStream fis = null; 83 ZipInputStream sourceZipStream; 84 85 FileOutputStream fos = null; 86 BufferedOutputStream targetStream; 87 88 ZipEntry theEntry = null; 89 String entryName = null; 90 91 try { 92 93 // 1. get the zipped input stream 94 fis = new FileInputStream(zipFile); 95 sourceZipStream = new ZipInputStream(fis); 96 97 // 2. get the zipped entries 98 while ( (theEntry = sourceZipStream.getNextEntry()) != null ) { 99 entryName = theEntry.getName(); 100 entryName = output.getPath().concat("/" + entryName); 101 102 // empty directory 103 // 104 if ( theEntry.isDirectory() ) { 105 continue; 106 } 107 // 3. prepare the uncompressed output stream 108 // 109 try { 110 fos = new FileOutputStream(entryName); 111 } 112 catch (FileNotFoundException exc) { 113 // the directory is not created...so let's build it! 114 // 115 buildDirectory(entryName); 116 fos = new FileOutputStream(entryName); 117 } 118 targetStream = new BufferedOutputStream(fos, DATA_BLOCK_SIZE); 119 120 int byteCount; 121 byte data[] = new byte[DATA_BLOCK_SIZE]; 122 123 // 4. read source zipped data and write to uncompressed stream 124 while ( (byteCount = sourceZipStream 125 .read(data, 0, DATA_BLOCK_SIZE)) != -1 ) { 126 127 targetStream.write(data, 0, byteCount); 128 } 129 // 5. close the target stream 130 // 131 targetStream.flush(); 132 targetStream.close(); 133 } 134 // close the source stream 135 // 136 sourceZipStream.close(); 137 138 } catch ( IOException exc ) { 139 exc.printStackTrace(); 140 } 141 } 142 143 /*** 144 * Creates the directory structure 145 */ 146 protected void buildDirectory(String entryName) throws IOException { 147 148 StringTokenizer st = new StringTokenizer(entryName, "/"); 149 150 int levels = st.countTokens() - 1; 151 StringBuffer directory = new StringBuffer(); 152 File newDir; 153 154 for (int i=0; i < levels; i++) { 155 String tmp = st.nextToken(); 156 directory.append(tmp + "/"); 157 } 158 newDir = new File(directory.toString()); 159 newDir.deleteOnExit(); 160 161 boolean created = newDir.mkdirs(); 162 if ( ! created ) { 163 LOG.error("Directory " + newDir + " cannot be created!!"); 164 } 165 } 166 }

This page was automatically generated by Maven