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
023/**
024 * Generic file name utilities.
025 * @since 1.20
026 */
027public class FileNameUtils {
028
029    private static String fileNameToBaseName(final String name) {
030        final int extensionIndex = name.lastIndexOf('.');
031        return extensionIndex < 0 ? name : name.substring(0, extensionIndex);
032    }
033
034    private static String fileNameToExtension(final String name) {
035        final int extensionIndex = name.lastIndexOf('.');
036        return extensionIndex < 0 ? "" : name.substring(extensionIndex + 1);
037    }
038
039    /**
040     * Gets the basename (i.e. the part up to and not including the
041     * last ".") of the last path segment of a filename.
042     * <p>Will return the file name itself if it doesn't contain any
043     * dots. All leading directories of the {@code filename} parameter
044     * are skipped.</p>
045     * @return the basename of filename
046     * @param path the path of the file to obtain the basename of.
047     * @since 1.22
048     */
049    public static String getBaseName(final Path path) {
050        if (path == null) {
051            return null;
052        }
053        final Path fileName = path.getFileName();
054        return fileName != null ? fileNameToBaseName(fileName.toString()) : null;
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        final Path fileName = path.getFileName();
090        return fileName != null ? fileNameToExtension(fileName.toString()) : null;
091    }
092
093    /**
094     * Gets the extension (i.e. the part after the last ".") of a file.
095     *
096     * <p>Will return an empty string if the file name doesn't contain
097     * any dots. Only the last segment of a the file name is consulted
098     * - i.e. all leading directories of the {@code filename}
099     * parameter are skipped.</p>
100     *
101     * @return the extension of filename
102     * @param filename the name of the file to obtain the extension of.
103     */
104    public static String getExtension(final String filename) {
105        if (filename == null) {
106            return null;
107        }
108        return fileNameToExtension(new File(filename).getName());
109    }
110}