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.archivers.zip;
019
020import static java.nio.charset.StandardCharsets.UTF_8;
021
022import java.nio.ByteBuffer;
023import java.nio.charset.Charset;
024import java.nio.charset.UnsupportedCharsetException;
025
026import org.apache.commons.compress.utils.CharsetNames;
027import org.apache.commons.compress.utils.Charsets;
028
029/**
030 * Static helper functions for robustly encoding file names in ZIP files.
031 */
032public abstract class ZipEncodingHelper {
033
034    /**
035     * UTF-8.
036     */
037    static final ZipEncoding ZIP_ENCODING_UTF_8 = getZipEncoding(CharsetNames.UTF_8);
038
039    /**
040     * Instantiates a ZIP encoding. An NIO based character set encoder/decoder will be returned.
041     * As a special case, if the character set is UTF-8, theNIOencoder will be configured  replace malformed and
042     * unmappable characters with '?'. This matches existing behavior from the older fallback encoder.
043     * <p>
044     *     If the requested character set cannot be found, the platform default will
045     *     be used instead.
046     * </p>
047     * @param name The name of the ZIP encoding. Specify {@code null} for
048     *             the platform's default encoding.
049     * @return A ZIP encoding for the given encoding name.
050     */
051    public static ZipEncoding getZipEncoding(final String name) {
052        Charset cs = Charset.defaultCharset();
053        try {
054            cs = Charsets.toCharset(name);
055        } catch (final UnsupportedCharsetException ignore) { // NOSONAR we use the default encoding instead
056        }
057        final boolean useReplacement = isUTF8(cs.name());
058        return new NioZipEncoding(cs, useReplacement);
059    }
060
061    static ByteBuffer growBufferBy(final ByteBuffer buffer, final int increment) {
062        buffer.limit(buffer.position());
063        buffer.rewind();
064
065        final ByteBuffer on = ByteBuffer.allocate(buffer.capacity() + increment);
066
067        on.put(buffer);
068        return on;
069    }
070
071    /**
072     * Tests whether a given encoding is UTF-8. If the given name is null, then check the platform's default encoding.
073     *
074     * @param charsetName If the given name is null, then check the platform's default encoding.
075     */
076    static boolean isUTF8(final String charsetName) {
077        final String actual = charsetName != null ? charsetName : Charset.defaultCharset().name();
078        if (UTF_8.name().equalsIgnoreCase(actual)) {
079            return true;
080        }
081        return UTF_8.aliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(actual));
082    }
083}