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 */ 018 019package org.apache.commons.compress.utils; 020 021import java.io.File; 022import java.nio.file.Path; 023 024/** 025 * Generic file name utilities. 026 * @since 1.20 027 */ 028public class FileNameUtils { 029 030 private static String fileNameToBaseName(final String name) { 031 final int extensionIndex = name.lastIndexOf('.'); 032 return extensionIndex < 0 ? name : name.substring(0, extensionIndex); 033 } 034 035 private static String fileNameToExtension(final String name) { 036 final int extensionIndex = name.lastIndexOf('.'); 037 return extensionIndex < 0 ? "" : name.substring(extensionIndex + 1); 038 } 039 040 /** 041 * Gets the basename (i.e. the part up to and not including the 042 * last ".") of the last path segment of a filename. 043 * <p>Will return the file name itself if it doesn't contain any 044 * dots. All leading directories of the {@code filename} parameter 045 * are skipped.</p> 046 * @return the basename of filename 047 * @param path the path of the file to obtain the basename of. 048 * @since 1.22 049 */ 050 public static String getBaseName(final Path path) { 051 if (path == null) { 052 return null; 053 } 054 return fileNameToBaseName(path.getFileName().toString()); 055 } 056 057 /** 058 * Gets the basename (i.e. the part up to and not including the 059 * last ".") of the last path segment of a filename. 060 * 061 * <p>Will return the file name itself if it doesn't contain any 062 * dots. All leading directories of the {@code filename} parameter 063 * are skipped.</p> 064 * 065 * @return the basename of filename 066 * @param filename the name of the file to obtain the basename of. 067 */ 068 public static String getBaseName(final String filename) { 069 if (filename == null) { 070 return null; 071 } 072 return fileNameToBaseName(new File(filename).getName()); 073 } 074 075 /** 076 * Gets the extension (i.e. the part after the last ".") of a file. 077 * <p>Will return an empty string if the file name doesn't contain 078 * any dots. Only the last segment of a the file name is consulted 079 * - i.e. all leading directories of the {@code filename} 080 * parameter are skipped.</p> 081 * @return the extension of filename 082 * @param path the path of the file to obtain the extension of. 083 * @since 1.22 084 */ 085 public static String getExtension(final Path path) { 086 if (path == null) { 087 return null; 088 } 089 return fileNameToExtension(path.getFileName().toString()); 090 } 091 092 /** 093 * Gets the extension (i.e. the part after the last ".") of a file. 094 * 095 * <p>Will return an empty string if the file name doesn't contain 096 * any dots. Only the last segment of a the file name is consulted 097 * - i.e. all leading directories of the {@code filename} 098 * parameter are skipped.</p> 099 * 100 * @return the extension of filename 101 * @param filename the name of the file to obtain the extension of. 102 */ 103 public static String getExtension(final String filename) { 104 if (filename == null) { 105 return null; 106 } 107 return fileNameToExtension(new File(filename).getName()); 108 } 109}