View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.compress.utils;
19  
20  import java.io.File;
21  import java.nio.file.Path;
22  
23  import org.apache.commons.io.FilenameUtils;
24  
25  /**
26   * Generic file name utilities.
27   *
28   * @since 1.20
29   */
30  public class FileNameUtils {
31  
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       */
42      public static String getBaseName(final Path path) {
43          // TODO Use Commons IO 2.16.0
44          if (path == null) {
45              return null;
46          }
47          final Path fileName = path.getFileName();
48          return fileName != null ? FilenameUtils.removeExtension(fileName.toString()) : null;
49      }
50  
51      /**
52       * Gets the base name (i.e. the part up to and not including the last ".") of the last path segment of a file name.
53       * <p>
54       * Will return the file name itself if it doesn't contain any dots. All leading directories of the {@code file name} parameter are skipped.
55       * </p>
56       *
57       * @return the base name of file name
58       * @param fileName the name of the file to obtain the base name of.
59       * @deprecated No longer used, no replacement.
60       */
61      @Deprecated
62      public static String getBaseName(final String fileName) {
63          if (fileName == null) {
64              return null;
65          }
66          return FilenameUtils.removeExtension(new File(fileName).getName());
67      }
68  
69      /**
70       * Gets the extension (i.e. the part after the last ".") of a file.
71       * <p>
72       * 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
73       * directories of the {@code file name} parameter are skipped.
74       * </p>
75       *
76       * @return the extension of file name
77       * @param path the path of the file to obtain the extension of.
78       * @since 1.22
79       */
80      public static String getExtension(final Path path) {
81          // TODO Use Commons IO 2.17.0
82          if (path == null) {
83              return null;
84          }
85          final Path fileName = path.getFileName();
86          return fileName != null ? FilenameUtils.getExtension(fileName.toString()) : null;
87      }
88  
89      /**
90       * Gets the extension (i.e. the part after the last ".") of a file.
91       * <p>
92       * 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
93       * directories of the {@code fileName} parameter are skipped.
94       * </p>
95       *
96       * @return the extension of file name
97       * @param fileName the name of the file to obtain the extension of.
98       * @deprecated Use {@link FilenameUtils#getExtension(String)}.
99       */
100     @Deprecated
101     public static String getExtension(final String fileName) {
102         return FilenameUtils.getExtension(fileName);
103     }
104 }