Java File Name Get getFileNames(String directory, String file)

Here you can find the source of getFileNames(String directory, String file)

Description

get File Names

License

BSD License

Declaration

@SuppressWarnings("unchecked")
    protected static Collection<String> getFileNames(String directory, String file) 

Method Source Code


//package com.java2s;
/*L//from  w  w w . j  a v  a  2  s .  co  m
 * Copyright The General Hospital Corporation d/b/a Massachusetts General Hospital
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/digital-model-repository/LICENSE.txt for details.
 */

import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    @SuppressWarnings("unchecked")
    protected static Collection<String> getFileNames(String directory, String file) {
        if (isArchive(file)) {
            try {
                ZipFile zip = new ZipFile(new File(directory + System.getProperty("file.separator") + file));
                Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) entries.nextElement();
                    if (isArchive(entry.getName())) {
                        return unzipZipFile(zip, directory);
                    }
                }
                zip.close();
            } catch (IOException e) {
                System.err.println("Tried to open " + file);
                e.printStackTrace();
            }
        }
        return Collections.singleton(file);
    }

    protected static boolean isArchive(String file) {
        if (file == null) {
            return false;
        }
        int periodIndex = file.lastIndexOf(".");
        return (file != null && periodIndex != -1
                && (".zip".equals(file.substring(periodIndex)) || ".jar".equals(file.substring(periodIndex))));
    }

    @SuppressWarnings("unchecked")
    private static Collection<String> unzipZipFile(ZipFile zip, String directory)
            throws FileNotFoundException, IOException {
        Collection<String> returnCollection = new ArrayList<String>();
        Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            copyInputStream(zip.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(directory + System.getProperty("file.separator") + entry.getName())));
            returnCollection.add(entry.getName());
        }
        zip.close();
        return returnCollection;
    }

    private static final void copyInputStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);

        in.close();
        out.close();
    }
}

Related

  1. getFilenames(File[] files)
  2. getFileNames(final String path, final String regex)
  3. getFileNames(final String pDataDir, final String pFilter)
  4. getFilenames(String dir, FilenameFilter filter)
  5. getFilenames(String dirctory)
  6. getFileNames(String dirPath)
  7. getFilenames(String filenames)
  8. getFileNames(String path, boolean withFullPath)
  9. getFileNames_STR(String path)