Java File Name Get getFileNames(File zipFile, Pattern pattern)

Here you can find the source of getFileNames(File zipFile, Pattern pattern)

Description

searches for all filenames that match the given regex-pattern

License

Open Source License

Parameter

Parameter Description
zipFile the zip file in which to search
pattern the pattern to search for

Return

all the filenames that match the pattern

Declaration

public static Collection<String> getFileNames(File zipFile, Pattern pattern) 

Method Source Code

//package com.java2s;
/*******************************************************************************
*
* This file is part of JMad./* ww  w  . j av a2  s. c  o  m*/
* 
* Copyright (c) 2008-2011, CERN. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* 
******************************************************************************/

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class Main {
    /**
     * searches for all filenames that match the given regex-pattern
     * 
     * @param zipFile the zip file in which to search
     * @param pattern the pattern to search for
     * @return all the filenames that match the pattern
     */
    public static Collection<String> getFileNames(File zipFile, Pattern pattern) {
        ZipFile theZipFile;
        try {
            theZipFile = new ZipFile(zipFile);
        } catch (ZipException e) {
            throw new Error(e);
        } catch (IOException e) {
            throw new Error(e);
        }
        return getFileNames(theZipFile, pattern);
    }

    public static Collection<String> getFileNames(ZipFile zipFile, Pattern pattern) {
        ArrayList<String> retval = new ArrayList<String>();
        Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
        while (zipEntries.hasMoreElements()) {
            ZipEntry zipEntry = zipEntries.nextElement();
            String fileName = zipEntry.getName();
            boolean accept = pattern.matcher(fileName).matches();
            if (accept) {
                retval.add(fileName);
            }
        }

        try {
            zipFile.close();
        } catch (IOException e1) {
            throw new Error(e1);
        }
        return retval;
    }
}

Related

  1. getFileNameOnlyFromFileObject(File fileToProcess)
  2. getFileNamePart(final File file)
  3. getFilenameParts(File file)
  4. getFileNames(byte[] zipFile)
  5. getFileNames(File path, Vector result)
  6. getFileNames(File[] files)
  7. getFilenames(File[] files)
  8. getFileNames(final String path, final String regex)
  9. getFileNames(final String pDataDir, final String pFilter)