Java BufferedInputStream Create getInputstreamForZipEntry(ZipFile zipFile, String uri)

Here you can find the source of getInputstreamForZipEntry(ZipFile zipFile, String uri)

Description

get Inputstream For Zip Entry

License

Open Source License

Declaration

public static InputStream getInputstreamForZipEntry(ZipFile zipFile,
            String uri) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001, 2010 IBM Corporation, Red Hat, and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  www  .  j a  v a  2  s .c o  m
 * IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

public class Main {
    public static InputStream getInputstreamForZipEntry(ZipFile zipFile,
            String uri) throws IOException {
        try {
            ZipEntry entry = zipFile.getEntry(uri);
            if (entry == null) {
                // this is a hack, but zip files are sensitive to the difference
                // between '/' and '\\'
                // so the hack is to try all combinations to see if any exist
                char[] chars = uri.toCharArray();
                int[] slashIndices = new int[chars.length];
                int slashCount = 0;
                for (int i = 0; i < uri.length(); i++) {
                    if (chars[i] == '/' || chars[i] == '\\') {
                        slashIndices[slashCount] = i;
                        slashCount++;
                    }
                }
                int slashPow = (int) Math.pow(2, slashCount);
                boolean foundIt = false;
                for (int i = 0; i < slashPow && !foundIt; i++) {
                    for (int j = 0; j < slashCount; j++) {
                        if ((i >> j & 1) == 1) {
                            chars[slashIndices[j]] = '/';
                        } else {
                            chars[slashIndices[j]] = '\\';
                        }
                    }
                    entry = zipFile.getEntry(new String(chars));
                    if (entry != null) {
                        foundIt = true;
                    }
                }
                if (entry == null) {
                    Exception del = new FileNotFoundException(uri);
                    throw new IOException(del.toString());
                }
            }
            return new java.io.BufferedInputStream(
                    zipFile.getInputStream(entry));
        } catch (IllegalStateException zipClosed) {
            throw new IOException(zipClosed.toString());
        }
    }
}

Related

  1. getInputStream(final String path)
  2. getInputStream(InputStream in)
  3. getInputStream(String path)
  4. getInputStream(String resource)
  5. getInputStream(String string)
  6. getInputStreamFromEncodedStr(String encodedDetails)
  7. getInputStreamFromString(String _string)
  8. getInputStreamFromString(String _string)
  9. getInputStreamFromString(String input)