// Path: libK3b/src/main/java/de/k3b/io/FileNameUtil.java
// public class FileNameUtil {
// 
//     /**
//      * converts baseName to a valid filename by replacing illegal chars.
//      * If it has no file-extension then defaultExtension is added
//      *
//      * @param defaultExtension the new extension, excluding the dot. null means no extension.
//      */
//     public static String createFileName(String baseName, String defaultExtension) {
//         StringBuilder result = new StringBuilder(baseName);
// 
//         // remove trailing "."
//         int len = result.length();
//         while ((len > 0) && (result.charAt(len - 1) == '.')) {
//             result.deleteCharAt(len - 1);
//             len--;
//         }
// 
//         // remove leading "."
//         while ((len > 0) && (result.charAt(0) == '.')) {
//             result.deleteCharAt(0);
//             len--;
//         }
// 
//         // add extension if there is none
//         if ((defaultExtension != null) && (result.indexOf(".") < 0)) {
//             result.append(".").append(defaultExtension);
//         }
// 
//         // replace illegal chars with "_"
//         replace(result, "_", "/", "\\", ":", " ", "?", "*", "&", "%", ">", "<", "|", "'", "\"", "__");
//         return result.toString();
//     }
// 
//     /**
//      * converts baseName to a valid filename by replacing illegal chars.
//      */
//     public static String createFileNameWitoutExtension(String fileNameCandidate) {
//         if (fileNameCandidate != null) {
//             // remove illegal chars or file extension
//             return FileNameUtil.createFileName(FileUtils.replaceExtension(fileNameCandidate, ""), null);
//         }
//         return null;
//     }
// 
//     public static String getWithoutWildcard(String path) {
//         if ((path == null) || (path.length() == 0)) return null;
//         if (path.endsWith("%")) {
//             // remove sql wildcard at end of name
//             return path.substring(0, path.length() - 1);
//         }
//         return path;
//     }
// 
//     /**
//      * converts filePath to a valid path by removing potential sql-wildcards and android specigic "//"
//      */
//     public static String fixPath(String filePath) {
//         String path = FileNameUtil.getWithoutWildcard(FileUtils.fixPath(filePath));
//         return path;
//     }
// 
//     /** replaces all occurences of illegalValues in result by replacement */
//     private static void replace(StringBuilder result, String replacement, String ... illegalValues) {
//         for (String illegalValue : illegalValues) {
//             int found = result.indexOf(illegalValue);
//             while (found >= 0) {
//                 result.replace(found, found+illegalValue.length(), replacement);
//                 found = result.indexOf(illegalValue);
//             }
//         }
//     }
// 
// 
//     /**
//      * so that files are comparable
//      */
//     public static String getCanonicalPath(File zipRelPath) {
//         File canonicalFile = FileUtils.tryGetCanonicalFile(zipRelPath);
//         if (canonicalFile != null) {
//             return FileUtils.fixPath(canonicalFile.getAbsolutePath());
//         }
//         return null;
//     }
// 
//     /**
//      * @return either srcFile without leading relativeToPath or null if srcFile is not relative to relativeToPath
//      */
//     public static String makePathRelative(String relativeToPath, File srcFile) {
//         String result = null;
//         if (!StringUtils.isNullOrEmpty(relativeToPath)) {
//             String srcPath = getCanonicalPath(srcFile);
//             boolean match = srcPath.toLowerCase().startsWith(relativeToPath);
//             if (match) {
//                 result = srcPath.substring(relativeToPath.length() + 1);
//             }
//         }
//         return result;
//     }
// }


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import de.k3b.io.FileNameUtil;
/*
 * Copyright (C) 2014-2019 k3b
 * 
 * This file is part of de.k3b.android.toGoZip (https://github.com/k3b/ToGoZip/) .
 * 
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details. 
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>
 */
package de.k3b.zip;




/**
 * One android independant, java.io.File based  dto-item that should be compressed.<br/>
 * <p/>
 * Author k3b
 */
public class FileCompressItem extends CompressItem {
    private static final Logger logger = LoggerFactory.getLogger(LibZipGlobal.LOG_TAG);
    private static final String DBG_CONTEXT = "FileCompressItem:";

    /** source file to be compressed */
    private File file;

    /** if not null file adds will be relative to this path if file is below this path */
    private static String zipRelPath = null;

    /**
     *
     * @param outDirInZipForNoneRelPath directory with trailing "/" (without filename) where
     *                                   the entry goes to if outside FileCompressItem.zipRelPath.
     *                                   null==root dir.
     * @param srcFile full path to source file
     * @param zipEntryComment
     */
    public FileCompressItem(String outDirInZipForNoneRelPath, File srcFile, String zipEntryComment) {
        String zipEntryName = calculateZipEntryName(outDirInZipForNoneRelPath, srcFile, FileCompressItem.zipRelPath);
        setFile(srcFile);
        setZipEntryFileName(zipEntryName);
        setZipEntryComment(zipEntryComment);
    }

    /**
     * Calculates the path within the zip file.
     *
     * Scope: package to allow unittesting.
     *
     * @param outDirInZipForNoneRelPath directory with trailing "/" (without filename) where
     *                                   the entry goes to if outside zipRelPath.
     *                                   null==root dir.
     * @param srcFile full path to source file
     * @param zipRelPath if not empty paths are caclulated relative to this directory.
     *                   Must have trailing "/" and be lower case.
     * @return
     */
    static String calculateZipEntryName(String outDirInZipForNoneRelPath, File srcFile, String zipRelPath) {
// Path: libK3b/src/main/java/de/k3b/io/FileNameUtil.java
// public class FileNameUtil {
// 
//     /**
//      * converts baseName to a valid filename by replacing illegal chars.
//      * If it has no file-extension then defaultExtension is added
//      *
//      * @param defaultExtension the new extension, excluding the dot. null means no extension.
//      */
//     public static String createFileName(String baseName, String defaultExtension) {
//         StringBuilder result = new StringBuilder(baseName);
// 
//         // remove trailing "."
//         int len = result.length();
//         while ((len > 0) && (result.charAt(len - 1) == '.')) {
//             result.deleteCharAt(len - 1);
//             len--;
//         }
// 
//         // remove leading "."
//         while ((len > 0) && (result.charAt(0) == '.')) {
//             result.deleteCharAt(0);
//             len--;
//         }
// 
//         // add extension if there is none
//         if ((defaultExtension != null) && (result.indexOf(".") < 0)) {
//             result.append(".").append(defaultExtension);
//         }
// 
//         // replace illegal chars with "_"
//         replace(result, "_", "/", "\\", ":", " ", "?", "*", "&", "%", ">", "<", "|", "'", "\"", "__");
//         return result.toString();
//     }
// 
//     /**
//      * converts baseName to a valid filename by replacing illegal chars.
//      */
//     public static String createFileNameWitoutExtension(String fileNameCandidate) {
//         if (fileNameCandidate != null) {
//             // remove illegal chars or file extension
//             return FileNameUtil.createFileName(FileUtils.replaceExtension(fileNameCandidate, ""), null);
//         }
//         return null;
//     }
// 
//     public static String getWithoutWildcard(String path) {
//         if ((path == null) || (path.length() == 0)) return null;
//         if (path.endsWith("%")) {
//             // remove sql wildcard at end of name
//             return path.substring(0, path.length() - 1);
//         }
//         return path;
//     }
// 
//     /**
//      * converts filePath to a valid path by removing potential sql-wildcards and android specigic "//"
//      */
//     public static String fixPath(String filePath) {
//         String path = FileNameUtil.getWithoutWildcard(FileUtils.fixPath(filePath));
//         return path;
//     }
// 
//     /** replaces all occurences of illegalValues in result by replacement */
//     private static void replace(StringBuilder result, String replacement, String ... illegalValues) {
//         for (String illegalValue : illegalValues) {
//             int found = result.indexOf(illegalValue);
//             while (found >= 0) {
//                 result.replace(found, found+illegalValue.length(), replacement);
//                 found = result.indexOf(illegalValue);
//             }
//         }
//     }
// 
// 
//     /**
//      * so that files are comparable
//      */
//     public static String getCanonicalPath(File zipRelPath) {
//         File canonicalFile = FileUtils.tryGetCanonicalFile(zipRelPath);
//         if (canonicalFile != null) {
//             return FileUtils.fixPath(canonicalFile.getAbsolutePath());
//         }
//         return null;
//     }
// 
//     /**
//      * @return either srcFile without leading relativeToPath or null if srcFile is not relative to relativeToPath
//      */
//     public static String makePathRelative(String relativeToPath, File srcFile) {
//         String result = null;
//         if (!StringUtils.isNullOrEmpty(relativeToPath)) {
//             String srcPath = getCanonicalPath(srcFile);
//             boolean match = srcPath.toLowerCase().startsWith(relativeToPath);
//             if (match) {
//                 result = srcPath.substring(relativeToPath.length() + 1);
//             }
//         }
//         return result;
//     }
// }


// Path: libK3bZip/src/main/java/de/k3b/zip/FileCompressItem.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import de.k3b.io.FileNameUtil;

/*
 * Copyright (C) 2014-2019 k3b
 * 
 * This file is part of de.k3b.android.toGoZip (https://github.com/k3b/ToGoZip/) .
 * 
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details. 
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>
 */
package de.k3b.zip;




/**
 * One android independant, java.io.File based  dto-item that should be compressed.<br/>
 * <p/>
 * Author k3b
 */
public class FileCompressItem extends CompressItem {
    private static final Logger logger = LoggerFactory.getLogger(LibZipGlobal.LOG_TAG);
    private static final String DBG_CONTEXT = "FileCompressItem:";

    /** source file to be compressed */
    private File file;

    /** if not null file adds will be relative to this path if file is below this path */
    private static String zipRelPath = null;

    /**
     *
     * @param outDirInZipForNoneRelPath directory with trailing "/" (without filename) where
     *                                   the entry goes to if outside FileCompressItem.zipRelPath.
     *                                   null==root dir.
     * @param srcFile full path to source file
     * @param zipEntryComment
     */
    public FileCompressItem(String outDirInZipForNoneRelPath, File srcFile, String zipEntryComment) {
        String zipEntryName = calculateZipEntryName(outDirInZipForNoneRelPath, srcFile, FileCompressItem.zipRelPath);
        setFile(srcFile);
        setZipEntryFileName(zipEntryName);
        setZipEntryComment(zipEntryComment);
    }

    /**
     * Calculates the path within the zip file.
     *
     * Scope: package to allow unittesting.
     *
     * @param outDirInZipForNoneRelPath directory with trailing "/" (without filename) where
     *                                   the entry goes to if outside zipRelPath.
     *                                   null==root dir.
     * @param srcFile full path to source file
     * @param zipRelPath if not empty paths are caclulated relative to this directory.
     *                   Must have trailing "/" and be lower case.
     * @return
     */
    static String calculateZipEntryName(String outDirInZipForNoneRelPath, File srcFile, String zipRelPath) {
        String result = FileNameUtil.makePathRelative(zipRelPath, srcFile);