FileNameUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   https://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */

  19. package org.apache.commons.compress.utils;

  20. import java.io.File;
  21. import java.nio.file.Path;

  22. import org.apache.commons.io.FilenameUtils;
  23. import org.apache.commons.io.file.PathUtils;

  24. /**
  25.  * Generic file name utilities.
  26.  *
  27.  * @since 1.20
  28.  * @deprecated Use {@link PathUtils} and {@link FilenameUtils}.
  29.  */
  30. @Deprecated
  31. public class FileNameUtils {

  32.     /**
  33.      * Gets the base name (i.e. the part up to and not including the last ".") of the last path segment of a file name.
  34.      * <p>
  35.      * Will return the file name itself if it doesn't contain any dots. All leading directories of the {@code file name} parameter are skipped.
  36.      * </p>
  37.      *
  38.      * @return the base name of file name
  39.      * @param path the path of the file to obtain the base name of.
  40.      * @since 1.22
  41.      * @deprecated Use {@link PathUtils#getBaseName(Path)}.
  42.      */
  43.     @Deprecated
  44.     public static String getBaseName(final Path path) {
  45.         return PathUtils.getBaseName(path);
  46.     }

  47.     /**
  48.      * Gets the base name (i.e. the part up to and not including the last ".") of the last path segment of a file name.
  49.      * <p>
  50.      * Will return the file name itself if it doesn't contain any dots. All leading directories of the {@code file name} parameter are skipped.
  51.      * </p>
  52.      *
  53.      * @return the base name of file name
  54.      * @param fileName the name of the file to obtain the base name of.
  55.      * @deprecated Use {@link FilenameUtils#removeExtension(String)}.
  56.      */
  57.     @Deprecated
  58.     public static String getBaseName(final String fileName) {
  59.         if (fileName == null) {
  60.             return null;
  61.         }
  62.         return FilenameUtils.removeExtension(new File(fileName).getName());
  63.     }

  64.     /**
  65.      * Gets the extension (i.e. the part after the last ".") of a file.
  66.      * <p>
  67.      * Will return an empty string if the file name doesn't contain any dots. Only the last segment of a the file name is consulted - i.e. all leading
  68.      * directories of the {@code file name} parameter are skipped.
  69.      * </p>
  70.      *
  71.      * @return the extension of file name
  72.      * @param path the path of the file to obtain the extension of.
  73.      * @since 1.22
  74.      * @deprecated Use {@link PathUtils#getExtension(Path)}.
  75.      */
  76.     @Deprecated
  77.     public static String getExtension(final Path path) {
  78.         return PathUtils.getExtension(path);
  79.     }

  80.     /**
  81.      * Gets the extension (i.e. the part after the last ".") of a file.
  82.      * <p>
  83.      * Will return an empty string if the file name doesn't contain any dots. Only the last segment of a the file name is consulted - i.e. all leading
  84.      * directories of the {@code fileName} parameter are skipped.
  85.      * </p>
  86.      *
  87.      * @return the extension of file name
  88.      * @param fileName the name of the file to obtain the extension of.
  89.      * @deprecated Use {@link FilenameUtils#getExtension(String)}.
  90.      */
  91.     @Deprecated
  92.     public static String getExtension(final String fileName) {
  93.         return FilenameUtils.getExtension(fileName);
  94.     }
  95. }