QingKeZhiXia
8/3/2019 - 12:31 PM

Java Utils

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * ZipUtils
 *
 * @author qingke.hk@gmail.com
 */
public class ZipUtils {
    /**
     * 将文件或目录压缩为一个 .zip 包,如果该 .zip 已经存在,则删除
     *
     * @param sources  源文件的绝对路径
     * @param target   zip 的绝对路径
     * @param excludes 压缩时忽略的文件
     * @throws IOException 删除旧 zip 时出错
     */
    public static void zip(String sources, String target, String... excludes) throws IOException {
        Path sourcesPath = Paths.get(sources);
        Path targetPath = Paths.get(target);

        Files.deleteIfExists(targetPath);

        try (ZipOutputStream zipStream = new ZipOutputStream(Files.newOutputStream(sourcesPath))) {
            Files.walkFileTree(sourcesPath, new SimpleFileVisitor<>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    File dirFile = dir.toFile();
                    File[] subFiles = dirFile.listFiles();
                    if (subFiles == null || subFiles.length == 0) {
                        zipStream.putNextEntry(new ZipEntry(dir.relativize(sourcesPath).toString() + File.separator));
                        zipStream.closeEntry();
                    }

                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (excludes != null) {
                        for (String exclude : excludes) {
                            if (file.endsWith(exclude)) {
                                return FileVisitResult.CONTINUE;
                            }
                        }
                    }

                    zipStream.putNextEntry(new ZipEntry(file.relativize(sourcesPath).toString()));
                    Files.copy(file, zipStream);
                    zipStream.closeEntry();

                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
}
public class StringUtils {
    /**
     * 翻转字符串中指定的部分
     *
     * @param str        源字符串
     * @param startIndex 起始位置
     * @param endIndex   结束位置
     * @return 翻转后的字符串
     */
    public static String reverseStr(String str, int startIndex, int endIndex) {
        // 参数检验
        if (str == null || str.length() == 0) {
            return null;
        }
        int length = str.length();
        if (startIndex > endIndex || startIndex >= length || endIndex < 0) {
            throw new IllegalArgumentException(String.format("参数输入错误,请仔细检查参数:startIndex - %d, endIndex - %d",
                    startIndex, endIndex));
        }

        // 开始翻转
        char[] chars = str.toCharArray();
        while (startIndex < endIndex) {
            char temp = chars[startIndex];
            chars[startIndex] = chars[endIndex];
            chars[endIndex] = temp;
            startIndex++;
            endIndex--;
        }

        return new String(chars);
    }
}
public class Reverseobjects {
    /**
     * 翻转整个对象数组
     *
     * @param objects 原对象数组
     * @return 翻转后的对象数组
     */
    public static Object[] reverse(Object[] objects) {
        if (objects == null || objects.length == 0) {
            return null;
        }

        return reverseObjects(objects, 0, objects.length - 1);
    }

    /**
     * 翻转数组中指定的部分
     *
     * @param objects    原数组
     * @param startIndex 起始位置
     * @param endIndex   结束位置
     * @return 翻转后的数组
     */
    public static Object[] reverseObjects(Object[] objects, int startIndex, int endIndex) {
        if (objects == null || objects.length == 0) {
            return null;
        }

        int length = objects.length;
        if (startIndex > endIndex || startIndex >= length || endIndex < 0) {
            throw new IllegalArgumentException(String.format("参数输入错误,请仔细检查参数:startIndex - %d, endIndex - %d",
                    startIndex, endIndex));
        }

        while (startIndex < endIndex) {
            Object temp = objects[startIndex];
            objects[startIndex] = objects[endIndex];
            objects[endIndex] = temp;

            startIndex++;
            endIndex--;
        }

        return objects;
    }
}