001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ecoinformatics.seek.datasource;
031
032import java.io.File;
033import java.io.FileOutputStream;
034import java.io.InputStream;
035import java.util.Enumeration;
036import java.util.zip.ZipEntry;
037import java.util.zip.ZipFile;
038
039import org.apache.commons.logging.Log;
040import org.apache.commons.logging.LogFactory;
041
042/**
043 * This class represents a zip data object. When the object downloaded, it will
044 * be unzipped into a sub cache directory
045 * 
046 * @author Jing Tao
047 * 
048 */
049
050public class EcogridZippedDataCacheItem extends EcogridCompressedDataCacheItem {
051        private static Log log;
052        static {
053                log = LogFactory
054                                .getLog("org.ecoinformatics.seek.datasource.EcogridZippedDataCacheItem");
055        }
056
057        /**
058         * Default constructor
059         */
060        public EcogridZippedDataCacheItem() {
061                super();
062        }
063
064        /**
065         * Constructor
066         * 
067         * @param refresh
068         *            if the compressed file need be uncompressed again even already
069         *            has umpressed before
070         */
071        public EcogridZippedDataCacheItem(boolean refresh) {
072                super(refresh);
073        }
074
075        /**
076         * This method overwirtes the super class. It is specified to unzip a zip
077         * file
078         * 
079         * @throws Exception
080         */
081        public void unCompressCacheItem() throws Exception {
082                if (unCompressedCacheItemDir != null) {
083                        log.debug("At unCompressCacheItem method in Zip ojbect");
084                        ZipFile zipFile = new ZipFile(getAbsoluteFileName());
085                        Enumeration enu = zipFile.entries();
086                        // go though every zip entry
087                        byte[] array = new byte[300 * 1024];
088                        while (enu.hasMoreElements()) {
089                                ZipEntry entry = (ZipEntry) enu.nextElement();
090                                // write zipEntry to a local file in
091                                // Cachedir/unzip/mlocatFilename/
092                                String name = entry.getName();
093                                if (name != null) {
094                                        log.debug("Zip entry name is " + name);
095                                        File unzipFile = new File(unCompressedCacheItemDir, name);
096                                        FileOutputStream fileWriter = new FileOutputStream(
097                                                        unzipFile);
098                                        InputStream fileReader = zipFile.getInputStream(entry);
099                                        int len;
100                                        while ((len = fileReader.read(array)) >= 0) {
101                                                fileWriter.write(array, 0, len);
102                                        }
103                                        fileReader.close();
104                                        fileWriter.close();
105                                }
106                        }
107                        unCompressedFileList = unCompressedCacheItemDir.list();
108                }
109
110        }
111
112}