001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.util;
022:
023: import org.apache.commons.io.IOUtils;
024: import java.io.OutputStream;
025: import java.io.InputStream;
026: import java.io.File;
027: import java.io.IOException;
028: import java.io.FileOutputStream;
029: import java.util.Enumeration;
030: import java.util.zip.ZipFile;
031: import java.util.zip.ZipEntry;
032: import java.util.zip.ZipException;
033: import com.methodhead.MhfException;
034:
035: public class MhfFileUtils {
036:
037: // constructors /////////////////////////////////////////////////////////////
038:
039: // constants ////////////////////////////////////////////////////////////////
040:
041: // classes //////////////////////////////////////////////////////////////////
042:
043: // methods //////////////////////////////////////////////////////////////////
044:
045: /**
046: * Unzips <tt>zipFile</tt> in <tt>destDir</tt>.
047: */
048: public static void unzip(
049: File zipFile,
050: File destDir )
051: throws
052: ZipException,
053: IOException {
054:
055: if ( zipFile == null )
056: throw new MhfException( "zipFile is null." );
057:
058: if ( destDir == null )
059: throw new MhfException( "destDir is null." );
060:
061: if ( !zipFile.exists() || !zipFile.isFile() )
062: throw new MhfException( "zipFile is not a valid file." );
063:
064: if ( !destDir.isDirectory() || !destDir.isDirectory() )
065: throw new MhfException( "destDir is not a valid directory." );
066:
067: ZipFile zf = new ZipFile( zipFile );
068:
069: //
070: // run through once to create directories (WinZip seems to create file
071: // entries before respective directory entries)
072: //
073: for ( Enumeration enum = zf.entries(); enum.hasMoreElements(); ) {
074: ZipEntry ze = ( ZipEntry )enum.nextElement();
075:
076: File f = new File( destDir, ze.getName() );
077: if ( ze.isDirectory() ) {
078: f.mkdirs();
079: }
080: }
081:
082: //
083: // run through again to extract files
084: //
085: for ( Enumeration enum = zf.entries(); enum.hasMoreElements(); ) {
086: ZipEntry ze = ( ZipEntry )enum.nextElement();
087:
088: File f = new File( destDir, ze.getName() );
089: if ( !ze.isDirectory() ) {
090: InputStream in = zf.getInputStream( ze );
091: OutputStream out = new FileOutputStream( f );
092: IOUtils.copy( in, out );
093: in.close();
094: out.close();
095: }
096: }
097:
098: zf.close();
099: }
100: // properties ///////////////////////////////////////////////////////////////
101:
102: // attributes ///////////////////////////////////////////////////////////////
103: }
|