001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.compress.utils; 019 020import java.io.File; 021import java.nio.file.Path; 022 023import org.apache.commons.io.FilenameUtils; 024 025/** 026 * Generic file name utilities. 027 * 028 * @since 1.20 029 */ 030public class FileNameUtils { 031 032 /** 033 * Gets the base name (i.e. the part up to and not including the last ".") of the last path segment of a file name. 034 * <p> 035 * Will return the file name itself if it doesn't contain any dots. All leading directories of the {@code file name} parameter are skipped. 036 * </p> 037 * 038 * @return the base name of file name 039 * @param path the path of the file to obtain the base name of. 040 * @since 1.22 041 */ 042 public static String getBaseName(final Path path) { 043 // TODO Use Commons IO 2.16.0 044 if (path == null) { 045 return null; 046 } 047 final Path fileName = path.getFileName(); 048 return fileName != null ? FilenameUtils.removeExtension(fileName.toString()) : null; 049 } 050 051 /** 052 * Gets the base name (i.e. the part up to and not including the last ".") of the last path segment of a file name. 053 * <p> 054 * Will return the file name itself if it doesn't contain any dots. All leading directories of the {@code file name} parameter are skipped. 055 * </p> 056 * 057 * @return the base name of file name 058 * @param fileName the name of the file to obtain the base name of. 059 * @deprecated No longer used, no replacement. 060 */ 061 @Deprecated 062 public static String getBaseName(final String fileName) { 063 if (fileName == null) { 064 return null; 065 } 066 return FilenameUtils.removeExtension(new File(fileName).getName()); 067 } 068 069 /** 070 * Gets the extension (i.e. the part after the last ".") of a file. 071 * <p> 072 * 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 073 * directories of the {@code file name} parameter are skipped. 074 * </p> 075 * 076 * @return the extension of file name 077 * @param path the path of the file to obtain the extension of. 078 * @since 1.22 079 */ 080 public static String getExtension(final Path path) { 081 // TODO Use Commons IO 2.17.0 082 if (path == null) { 083 return null; 084 } 085 final Path fileName = path.getFileName(); 086 return fileName != null ? FilenameUtils.getExtension(fileName.toString()) : null; 087 } 088 089 /** 090 * Gets the extension (i.e. the part after the last ".") of a file. 091 * <p> 092 * 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 093 * directories of the {@code fileName} parameter are skipped. 094 * </p> 095 * 096 * @return the extension of file name 097 * @param fileName the name of the file to obtain the extension of. 098 * @deprecated Use {@link FilenameUtils#getExtension(String)}. 099 */ 100 @Deprecated 101 public static String getExtension(final String fileName) { 102 return FilenameUtils.getExtension(fileName); 103 } 104}