Tools.java 文件源码

java
阅读 44 收藏 0 点赞 0 评论 0

项目:rapidminer 作者:
/**
 * This method checks if the given file is a Zip file containing one entry (in case of file
 * extension .zip). If this is the case, a reader based on a ZipInputStream for this entry is
 * returned. Otherwise, this method checks if the file has the extension .gz. If this applies, a
 * gzipped stream reader is returned. Otherwise, this method just returns a BufferedReader for
 * the given file (file was not zipped at all).
 */
public static BufferedReader getReader(File file, Charset encoding) throws IOException {
    // handle zip files if necessary
    if (file.getAbsolutePath().endsWith(".zip")) {
        try (ZipFile zipFile = new ZipFile(file)) {
            if (zipFile.size() == 0) {
                throw new IOException("Input of Zip file failed: the file archive does not contain any entries.");
            }
            if (zipFile.size() > 1) {
                throw new IOException("Input of Zip file failed: the file archive contains more than one entry.");
            }
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            InputStream zipIn = zipFile.getInputStream(entries.nextElement());
            return new BufferedReader(new InputStreamReader(zipIn, encoding));
        }
    } else if (file.getAbsolutePath().endsWith(".gz")) {
        return new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), encoding));
    } else {
        return new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号