001: /* IoUtils
002: *
003: * $Id: IoUtils.java 3716 2005-07-20 22:21:59Z stack-sf $
004: *
005: * Created on Jun 9, 2005
006: *
007: * Copyright (C) 2003 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026: package org.archive.crawler.util;
027:
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.FileNotFoundException;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.net.URL;
034:
035: import org.apache.tools.ant.Project;
036: import org.apache.tools.ant.Target;
037: import org.apache.tools.ant.taskdefs.Expand;
038: import org.archive.net.UURI;
039:
040: /**
041: * Logging utils.
042: * @author stack
043: */
044: public class IoUtils {
045: public static InputStream getInputStream(String pathOrUrl) {
046: return getInputStream(null, pathOrUrl);
047: }
048:
049: /**
050: * Get inputstream.
051: *
052: * This method looks at passed string and tries to judge it a
053: * filesystem path or an URL. It then gets an InputStream on to
054: * the file or URL.
055: *
056: * <p>ASSUMPTION: Scheme on any url will probably only ever be 'file'
057: * or 'http'.
058: *
059: * @param basedir If passed <code>fileOrUrl</code> is a file path and
060: * it is not absolute, prefix with this basedir (May be null then
061: * no prefixing will be done).
062: * @param pathOrUrl Pass path to a file on disk or pass in a URL.
063: * @return An input stream.
064: */
065: public static InputStream getInputStream(File basedir,
066: String pathOrUrl) {
067: InputStream is = null;
068: if (UURI.hasScheme(pathOrUrl)) {
069: try {
070: URL url = new URL(pathOrUrl);
071: is = url.openStream();
072: } catch (IOException e) {
073: e.printStackTrace();
074: }
075: } else {
076: // Assume its not an URI or we failed the parse.
077: // Try it as a file.
078: File source = new File(pathOrUrl);
079: if (!source.isAbsolute() && basedir != null) {
080: source = new File(basedir, pathOrUrl);
081: }
082: try {
083: is = new FileInputStream(source);
084: } catch (FileNotFoundException e) {
085: e.printStackTrace();
086: }
087: }
088: return is;
089: }
090:
091: /**
092: * Use ant to unjar.
093: * @param zipFile File to unzip.
094: * @param destinationDir Where to unzip to.
095: */
096: public static void unzip(File zipFile, File destinationDir) {
097: unzip(zipFile, destinationDir, true);
098: }
099:
100: /**
101: * Use ant to unjar.
102: * @param zipFile File to unzip.
103: * @param destinationDir Where to unzip to.
104: * @param overwrite Whether to overwrite existing content.
105: */
106: public static void unzip(File zipFile, File destinationDir,
107: boolean overwrite) {
108: final class Expander extends Expand {
109: public Expander() {
110: }
111: }
112: Expander expander = new Expander();
113: expander.setProject(new Project());
114: expander.getProject().init();
115: expander.setTaskType("unzip");
116: expander.setTaskName("unzip");
117: expander.setOwningTarget(new Target());
118: expander.setSrc(zipFile);
119: expander.setDest(destinationDir);
120: expander.setOverwrite(overwrite);
121: expander.execute();
122: }
123: }
|