All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soento.core.util.FileUtil Maven / Gradle / Ivy

package com.soento.core.util;

import com.soento.core.enums.Audio;
import com.soento.core.enums.Image;
import com.soento.core.enums.Video;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author soento
 */
public class FileUtil extends FileUtils {

    public static void zip(File zip, String srcPathName) {
        File file = new File(srcPathName);
        if (!file.exists()) {
            throw new RuntimeException(srcPathName + "不存在!");
        }
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new CheckedOutputStream(new FileOutputStream(zip), new CRC32()));
            String basedir = "";
            compress(file, out, basedir);
            out.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            StreamUtil.close(out);
        }
    }

    private static void compress(File file, ZipOutputStream out, String basedir) {
        /* 判断是目录还是文件 */
        if (file.isDirectory()) {
            compressDirectory(file, out, basedir);
        } else {
            compressFile(file, out, basedir);
        }
    }

    /**
     * 压缩一个目录
     */
    private static void compressDirectory(File dir, ZipOutputStream out, String basedir) {
        if (!dir.exists()) {
            return;
        }
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                compress(files[i], out, basedir + dir.getName() + File.separator);
            }
        }
    }

    private static void compressFile(File file, ZipOutputStream out, String basedir) {
        if (!file.exists()) {
            return;
        }
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(basedir + file.getName());
            out.putNextEntry(entry);
            int count;
            final int maxLength = 8192;
            byte[] data = new byte[maxLength];
            while ((count = bis.read(data, 0, maxLength)) != -1) {
                out.write(data, 0, count);
            }
            bis.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] toByteArray(File f) {
        if (!f.exists()) {
            throw new RuntimeException(f.getPath());
        }
        FileChannel channel = null;
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(f);
            channel = fs.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
            while ((channel.read(byteBuffer)) > 0) {
            }
            return byteBuffer.array();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            StreamUtil.close(channel);
            StreamUtil.close(fs);
        }
    }

    /**
     * 根据给定的文件名,获取其扩展名
     *
     * @param filename 文件名
     * @return 扩展名
     */
    public static String extension(String filename) {
        return FilenameUtils.getExtension(filename);
    }

    /**
     * 根据给定的扩展名,获取文件类型
     *
     * @param extension 扩展名
     * @return 文件类型
     */
    public static String type(String extension) {
        if (Image.has(extension)) {
            return "image";
        } else if (Audio.has(extension)) {
            return "audio";
        } else if (Video.has(extension)) {
            return "video";
        } else {
            return "file";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy