Java FileInputStream Create getStreamForFile(String path)

Here you can find the source of getStreamForFile(String path)

Description

get Stream For File

License

Open Source License

Declaration

public static InputStream getStreamForFile(String path) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.zip.ZipFile;

public class Main {
    private static HashMap<String, ZipFile> zipFiles = new HashMap<String, ZipFile>();
    public static LinkedList<String> alternateFiles = new LinkedList<String>();

    public static InputStream getStreamForFile(String zipFile, String file) {
        InputStream toReturn = null;
        if (!new File(zipFile.split("\\.bin")[0] + "/" + file).exists()) {
            if (!zipFiles.containsKey(zipFile))
                prepareZip(zipFile);/*from  www . j a va  2 s .  com*/
            ZipFile zipObj = zipFiles.get(zipFile);
            try {
                toReturn = zipObj.getInputStream(zipObj.getEntry(file));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                System.err.println("Local version of " + zipFile + "/" + file + " used");
                toReturn = new FileInputStream(new File(zipFile.split("\\.bin")[0] + "/" + file));
                if (!alternateFiles.contains(file))
                    alternateFiles.add(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return toReturn;
    }

    public static InputStream getStreamForFile(String path) {
        String zipFile = "";
        for (String segment : path.replace("\\", "/").split("/")) {
            zipFile += segment + "/";
            if (segment.contains(".bin"))
                break;
        }
        String file = path.substring(zipFile.length());

        return getStreamForFile(zipFile, file);
    }

    public static boolean exists(String zipFile, String file) {
        if (!new File(zipFile.split("\\.bin")[0] + "/" + file).exists()) {
            if (!zipFiles.containsKey(zipFile) && new File(zipFile).exists())
                prepareZip(zipFile);
            else if (!new File(zipFile).exists())
                return false;
            return zipFiles.get(zipFile).getEntry(file) != null;
        } else {
            return new File(zipFile.split("\\.bin")[0] + "/" + file).exists();
        }
    }

    private static void prepareZip(String archive) {
        if (!zipFiles.containsKey(archive))
            try {
                zipFiles.put(archive, new ZipFile(archive));
            } catch (IOException e) {
                System.err.println("An error occured loading " + archive + "!");
                e.printStackTrace();
                System.exit(1);
            }
    }
}

Related

  1. getStream(String fileName, boolean resource)
  2. getStreamForFile(String fileName)
  3. getStreamFromFile(File file)