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.FileInputStream; 034import java.io.FileOutputStream; 035import java.util.zip.GZIPInputStream; 036 037import org.apache.commons.logging.Log; 038import org.apache.commons.logging.LogFactory; 039 040/** 041 * This class reprents a gzipped cache item, it will run gunzip to uncompressed 042 * this data object 043 * 044 * @author Jing Tao 045 * 046 */ 047public class EcogridGZippedDataCacheItem extends EcogridCompressedDataCacheItem { 048 private static Log log; 049 static { 050 log = LogFactory 051 .getLog("org.ecoinformatics.seek.datasource.EcogridGZippedDataCacheItem"); 052 } 053 054 /** 055 * Default constructor for this object 056 */ 057 public EcogridGZippedDataCacheItem() { 058 super(); 059 } 060 061 /** 062 * Constructor 063 * 064 * @param refresh 065 * if the compressed file need be uncompressed again even already 066 * has umpressed before 067 */ 068 public EcogridGZippedDataCacheItem(boolean refresh) { 069 super(refresh); 070 } 071 072 /** 073 * This method overwrite the super class - EcogridCompressedDataCacheItem. 074 * It specifys the gunzip method to uncompress the file. The new ungzip file 075 * will be the the cacheitem file name without the file extension. An file 076 * location will be the cachedata/unzip/cacheitemname/ If this is tar file 077 * too, we will untar it after ungzip it. 078 * 079 * @throws Exception 080 */ 081 public void unCompressCacheItem() throws Exception { 082 if (unCompressedFilePath != null) { 083 log.debug("At unCompressCacheItem method in Zip ojbect"); 084 // read the gzip file and ungzip it 085 GZIPInputStream gZipFileReader = new GZIPInputStream( 086 new FileInputStream(getFile())); 087 String unGZipFileName = removeFileExtension(getAbsoluteFileName()); 088 String unGZipFilePath = unCompressedFilePath + File.separator 089 + unGZipFileName; 090 log.debug("The unGzip aboslute file path is " + unGZipFilePath); 091 File unGzipFile = new File(unGZipFilePath); 092 FileOutputStream fileWriter = new FileOutputStream(unGzipFile); 093 byte[] array = new byte[3000 * 1024]; 094 int len; 095 while ((len = gZipFileReader.read(array)) >= 0) { 096 fileWriter.write(array, 0, len); 097 } 098 gZipFileReader.close(); 099 fileWriter.close(); 100 // if this is a tar file too, will untar it. 101 if (getIsTarFile()) { 102 if (unCompressedFilePath != null) { 103 log.debug("untar the file after ungzip"); 104 EcogridTarArchivedDataCacheItem.extractTarFile( 105 unGZipFilePath, unCompressedCacheItemDir); 106 } 107 } 108 unCompressedFileList = unCompressedCacheItemDir.list(); 109 } 110 } 111 112 /* 113 * Given a file name, this method will remove file extension. If no file 114 * extension the given string will be returned. 115 */ 116 private String removeFileExtension(String fileName) { 117 String newFileName = null; 118 if (fileName != null) { 119 int lastDotIndex = fileName.lastIndexOf("."); 120 if (lastDotIndex != -1) { 121 newFileName = fileName.substring(0, lastDotIndex); 122 } else { 123 newFileName = fileName; 124 } 125 } 126 log.debug("The ungzip file name will be " + newFileName); 127 return newFileName; 128 } 129 130}