001: /*
002: * CompressedFile.java
003: *
004: * Created on December 12, 2002, 9:51 AM
005: */
006: /*
007: * The Unified Mapping Platform (JUMP) is an extensible, interactive
008: * GUI for visualizing and manipulating spatial features with geometry
009: * and attributes.
010: *
011: * Copyright (C) 2003 Vivid Solutions
012: *
013: * This program is free software; you can redistribute it and/or
014: * modify it under the terms of the GNU General Public License
015: * as published by the Free Software Foundation; either version 2
016: * of the License, or (at your option) any later version.
017: *
018: * This program is distributed in the hope that it will be useful,
019: * but WITHOUT ANY WARRANTY; without even the implied warranty of
020: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: * GNU General Public License for more details.
022: *
023: * You should have received a copy of the GNU General Public License
024: * along with this program; if not, write to the Free Software
025: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
026: *
027: * For more information, contact:
028: *
029: * Vivid Solutions
030: * Suite #1A
031: * 2328 Government Street
032: * Victoria BC V8T 5G5
033: * Canada
034: *
035: * (250)385-6040
036: * www.vividsolutions.com
037: */
038:
039: package com.vividsolutions.jump.io;
040:
041: import java.io.FileInputStream;
042: import java.io.InputStream;
043: import java.util.zip.ZipEntry;
044: import java.util.zip.ZipInputStream;
045:
046: /**
047: * Utility class for dealing with compressed (.zip, .gz) files.
048: *
049: * @author dblasby
050: * @TODO :I18N
051: */
052: public class CompressedFile {
053:
054: /** Creates a new CompressedFile */
055: public CompressedFile() {
056: }
057:
058: /**
059: * Searches through the .zip file looking for a file with the
060: * given extension. Returns null if it doesn't find one.
061: */
062: public static String getInternalZipFnameByExtension(
063: String extension, String compressedFile) throws Exception {
064: //zip file
065: String inside_zip_extension;
066: InputStream IS_low = new FileInputStream(compressedFile);
067: ZipInputStream fr_high = new ZipInputStream(IS_low);
068:
069: //need to find the correct file within the .zip file
070: ZipEntry entry;
071:
072: entry = fr_high.getNextEntry();
073:
074: while (entry != null) {
075: inside_zip_extension = entry.getName().substring(
076: entry.getName().length() - extension.length());
077:
078: if (inside_zip_extension.compareToIgnoreCase(extension) == 0) {
079: return (entry.getName());
080: }
081:
082: entry = fr_high.getNextEntry();
083: }
084:
085: return null;
086: }
087:
088: /**
089: * Utility file open function - handles compressed and
090: * un-compressed files.
091: *
092: * @param compressedFile name of file to open.
093: * @param extension look for file with this extension in zipfile.
094: *
095: * <p>
096: * compressedFile = null, then opens a FileInputStream on
097: * COMPRESSEDFILE (!) should be file.shp or such
098: * </p>
099: *
100: * <p>
101: * compressedFile ends in ".zip" - opens the compressed zipfile
102: * and lookes for first file with extension 'extension'
103: * </p>
104: *
105: * <p>
106: * compressedFile ends in ".gz" - opens the compressed .gz file.
107: * </p>
108: */
109: static InputStream openFileExtension(String extension,
110: String compressedFile) throws Exception {
111: String compressed_extension;
112: String inside_zip_extension;
113:
114: if ((compressedFile == null) || (compressedFile.length() == 0)) {
115: throw new Exception(
116: "openFileExtension- no compressed file given.");
117: }
118:
119: compressed_extension = compressedFile.substring(compressedFile
120: .length() - 3);
121:
122: if (compressed_extension.compareToIgnoreCase(".gz") == 0) {
123: // gz file -- easy
124:
125: // low-level reader
126: InputStream IS_low = new FileInputStream(compressedFile);
127:
128: // high-level reader
129: return (new java.util.zip.GZIPInputStream(IS_low));
130: }
131:
132: if (compressed_extension.compareToIgnoreCase("zip") == 0) {
133: //zip file
134: InputStream IS_low = new FileInputStream(compressedFile);
135: ZipInputStream fr_high = new ZipInputStream(IS_low);
136:
137: //need to find the correct file within the .zip file
138: ZipEntry entry;
139:
140: entry = fr_high.getNextEntry();
141:
142: while (entry != null) {
143: inside_zip_extension = entry.getName().substring(
144: entry.getName().length() - extension.length());
145:
146: if (inside_zip_extension.compareToIgnoreCase(extension) == 0) {
147: return (fr_high);
148: }
149:
150: entry = fr_high.getNextEntry();
151: }
152:
153: throw new Exception("couldnt find file with extension"
154: + extension + " in compressed file "
155: + compressedFile);
156: }
157:
158: throw new Exception(
159: "couldnt determine compressed file type for file "
160: + compressedFile
161: + "- should end in .zip or .gz");
162: }
163:
164: /**
165: * Utility file open function - handles compressed and un-compressed files.
166: *
167: * @param fname name of the file to search for.
168: * @param compressedFile name of the compressed file.
169: *
170: * <p>
171: * If compressedFile = null, opens a FileInputStream on fname
172: * </p>
173: *
174: * <p>
175: * compressedFile ends in ".zip" - opens the compressed Zip and
176: * lookes for the file called fname
177: * </p>
178: *
179: * <p>
180: * compressedFile ends in ".gz" - opens the compressed .gz file.
181: * </p>
182: */
183: static InputStream openFile(String fname, String compressedFile)
184: throws Exception {
185: String extension;
186:
187: if ((compressedFile == null) || (compressedFile.length() == 0)) {
188: return new FileInputStream(fname);
189: }
190:
191: extension = compressedFile
192: .substring(compressedFile.length() - 3);
193:
194: if (extension.compareToIgnoreCase(".gz") == 0) {
195: // gz file -- easy
196:
197: // low-level reader
198: InputStream IS_low = new FileInputStream(compressedFile);
199:
200: // high-level reader
201: return (new java.util.zip.GZIPInputStream(IS_low));
202: }
203:
204: if (extension.compareToIgnoreCase("zip") == 0) {
205: //zip file
206: InputStream IS_low = new FileInputStream(compressedFile);
207: ZipInputStream fr_high = new ZipInputStream(IS_low);
208:
209: //need to find the correct file within the .zip file
210: ZipEntry entry;
211:
212: entry = fr_high.getNextEntry();
213:
214: while (entry != null) {
215: if (entry.getName().compareToIgnoreCase(fname) == 0) {
216: return (fr_high);
217: }
218:
219: entry = fr_high.getNextEntry();
220: }
221:
222: throw new Exception("couldnt find " + fname
223: + " in compressed file " + compressedFile);
224: }
225:
226: throw new Exception(
227: "Couldn't determine compressed file type for file "
228: + compressedFile
229: + " - Should end in .zip or .gz");
230: }
231: }
|