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 final Path fileName = path.getFileName(); 055 return fileName != null ? fileNameToBaseName(fileName.toString()) : null; 056 } 057 058 /** 059 * Gets the basename (i.e. the part up to and not including the 060 * last ".") of the last path segment of a filename. 061 * 062 * <p>Will return the file name itself if it doesn't contain any 063 * dots. All leading directories of the {@code filename} parameter 064 * are skipped.</p> 065 * 066 * @return the basename of filename 067 * @param filename the name of the file to obtain the basename of. 068 */ 069 public static String getBaseName(final String filename) { 070 if (filename == null) { 071 return null; 072 } 073 return fileNameToBaseName(new File(filename).getName()); 074 } 075 076 /** 077 * Gets the extension (i.e. the part after the last ".") of a file. 078 * <p>Will return an empty string if the file name doesn't contain 079 * any dots. Only the last segment of a the file name is consulted 080 * - i.e. all leading directories of the {@code filename} 081 * parameter are skipped.</p> 082 * @return the extension of filename 083 * @param path the path of the file to obtain the extension of. 084 * @since 1.22 085 */ 086 public static String getExtension(final Path path) { 087 if (path == null) { 088 return null; 089 } 090 final Path fileName = path.getFileName(); 091 return fileName != null ? fileNameToExtension(fileName.toString()) : null; 092 } 093 094 /** 095 * Gets the extension (i.e. the part after the last ".") of a file. 096 * 097 * <p>Will return an empty string if the file name doesn't contain 098 * any dots. Only the last segment of a the file name is consulted 099 * - i.e. all leading directories of the {@code filename} 100 * parameter are skipped.</p> 101 * 102 * @return the extension of filename 103 * @param filename the name of the file to obtain the extension of. 104 */ 105 public static String getExtension(final String filename) { 106 if (filename == null) { 107 return null; 108 } 109 return fileNameToExtension(new File(filename).getName()); 110 } 111}