ZipUtil.java

  1. // @formatter:off
  2.  /*******************************************************************************
  3.  *
  4.  * This file is part of JMad.
  5.  *
  6.  * Copyright (c) 2008-2011, CERN. All rights reserved.
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  *
  20.  ******************************************************************************/
  21. // @formatter:on

  22. /**
  23.  *
  24.  */
  25. package cern.accsoft.steering.jmad.util;

  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.Enumeration;
  31. import java.util.regex.Pattern;
  32. import java.util.zip.ZipEntry;
  33. import java.util.zip.ZipException;
  34. import java.util.zip.ZipFile;

  35. /**
  36.  * some utility methods for zip files
  37.  *
  38.  * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  39.  */
  40. public final class ZipUtil {

  41.     /**
  42.      * private constructor to prevent instantiation
  43.      */
  44.     private ZipUtil() {
  45.         /* Only static methods */
  46.     }

  47.     /**
  48.      * searches for all filenames that match the given regex-pattern
  49.      *
  50.      * @param zipFile the zip file in which to search
  51.      * @param pattern the pattern to search for
  52.      * @return all the filenames that match the pattern
  53.      */
  54.     public static Collection<String> getFileNames(File zipFile, Pattern pattern) {
  55.         ZipFile theZipFile;
  56.         try {
  57.             theZipFile = new ZipFile(zipFile);
  58.         } catch (ZipException e) {
  59.             throw new Error(e);
  60.         } catch (IOException e) {
  61.             throw new Error(e);
  62.         }
  63.         return getFileNames(theZipFile, pattern);
  64.     }

  65.     public static Collection<String> getFileNames(ZipFile zipFile, Pattern pattern) {
  66.         ArrayList<String> retval = new ArrayList<String>();
  67.         Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
  68.         while (zipEntries.hasMoreElements()) {
  69.             ZipEntry zipEntry = zipEntries.nextElement();
  70.             String fileName = zipEntry.getName();
  71.             boolean accept = pattern.matcher(fileName).matches();
  72.             if (accept) {
  73.                 retval.add(fileName);
  74.             }
  75.         }

  76.         try {
  77.             zipFile.close();
  78.         } catch (IOException e1) {
  79.             throw new Error(e1);
  80.         }
  81.         return retval;
  82.     }
  83. }