zipUtils工具类相关

import com.google.common.base.Strings;import com.google.common.collect.Lists;import com.ibbc.common.utils.FastDFSUtils;import org.apache.commons.compress.archivers.ArchiveEntry;import org.apache.commons.compress.archivers.zip.Zip64Mode;import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;import org.apache.commons.io.FileUtils;import org.apache.commons.lang.exception.ExceptionUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.*;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @author wanglamao * @date 2022/08/01public class ZipUtils {    private static final Logger LOGGER = LoggerFactory.getLogger(ZipUtils.class);    /**     * 将文件打包成zip压缩包文件     *     * @param files              要压缩的文件     * @param zipFile            压缩文件     * @param deleteFileAfterZip 压缩文件后删除原来的文件,临时文件时记得删除     * @return 是否压缩成功     */    public static boolean zip(List files, File zipFile, boolean deleteFileAfterZip) {        InputStream inputStream = null;        ZipArchiveOutputStream zipArchiveOutputStream = null;        try {            zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);            zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);            for (File file : files) {                ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());                zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);                inputStream = new FileInputStream(file);                byte[] buffer = new byte[1024 * 8];                int len = -1;                while ((len = inputStream.read(buffer)) != -1) {                    zipArchiveOutputStream.write(buffer, 0, len);                }                inputStream.close();            }            zipArchiveOutputStream.closeArchiveEntry();            zipArchiveOutputStream.finish();            if (deleteFileAfterZip) {                for (File file : files) {                    file.deleteOnExit();                }            }        } catch (IOException e) {            LOGGER.error("IOException:{}", ExceptionUtils.getFullStackTrace(e));            return false;        } finally {            try {                if (null != inputStream) {                    inputStream.close();                }                //关闭输出流                if (null != zipArchiveOutputStream) {                    zipArchiveOutputStream.close();                }            } catch (IOException e) {                LOGGER.error("IOException:{}", ExceptionUtils.getFullStackTrace(e));            }        }        return true;    }    /**     * 将zip压缩包解压成文件到指定文件夹     *     * @param zipFilePath   要解压的文件     * @param targetDirPath 解压的目的路径     * @return 是否成功     */    public static List unzip(String zipFilePath, String targetDirPath) throws Exception {        InputStream inputStream = null;        //zip文件输入流        try {            File zipFile = new File(zipFilePath);            inputStream = new FileInputStream(zipFile);            return unzip(inputStream, targetDirPath);        } catch (IOException e) {            LOGGER.error("IOException:{}", ExceptionUtils.getFullStackTrace(e));            throw e;        } finally {            try {                if (null != inputStream) {                    inputStream.close();                }            } catch (IOException e) {                LOGGER.error("IOException:{}", ExceptionUtils.getFullStackTrace(e));                throw e;            }        }    }    public static List unzip(InputStream inputStream, String targetDirPath) throws Exception {        OutputStream outputStream = null;        //zip文件输入流        ZipArchiveInputStream zipArchiveInputStream = null;        ArchiveEntry archiveEntry = null;        try {            List fileList = Lists.newArrayList();            zipArchiveInputStream = new ZipArchiveInputStream(inputStream, "UTF-8");            while (null != (archiveEntry = zipArchiveInputStream.getNextEntry())) {                String archiveEntryFileName = archiveEntry.getName();                LOGGER.debug("archiveEntryFileName:{}", archiveEntryFileName);                if (archiveEntry.isDirectory()) {                    createDirectory(targetDirPath, archiveEntryFileName);                } else {                    String archiveEntryPath = targetDirPath + archiveEntryFileName;                    fileList.add(archiveEntryPath);                    File entryFile = new File(archiveEntryPath);                    if (!entryFile.exists()) {                        entryFile.getParentFile().mkdirs();                    }                    byte[] buffer = new byte[1024 * 8];                    outputStream = new FileOutputStream(entryFile);                    int len = -1;                    while ((len = zipArchiveInputStream.read(buffer)) != -1) {                        outputStream.write(buffer, 0, len);                    }                    outputStream.flush();                    outputStream.close();                }            }            return fileList;        } catch (IOException e) {            LOGGER.error("IOException:{}", ExceptionUtils.getFullStackTrace(e));            throw e;        } finally {            try {                if (null != outputStream) {                    outputStream.close();                }                if (null != zipArchiveInputStream) {                    zipArchiveInputStream.close();                }            } catch (IOException e) {                LOGGER.error("IOException:{}", ExceptionUtils.getFullStackTrace(e));            }        }    }    public static Map unZipAndUpload2Fdfs(InputStream inputStream, String targetDirPath) throws IOException {        OutputStream outputStream = null;        //zip文件输入流        ZipArchiveInputStream zipArchiveInputStream = null;        ArchiveEntry archiveEntry = null;        Map fileInfoMap = new HashMap<>();        try {            zipArchiveInputStream = new ZipArchiveInputStream(inputStream, "UTF-8");            while (null != (archiveEntry = zipArchiveInputStream.getNextEntry())) {                //获取文件名                String archiveEntryFileName = archiveEntry.getName();                LOGGER.debug("archiveEntryFileName:{}", archiveEntryFileName);                if (archiveEntry.isDirectory()) {                    createDirectory(targetDirPath, archiveEntryFileName);                } else {                    String archiveEntryPath = targetDirPath + archiveEntryFileName;                    File entryFile = new File(archiveEntryPath);                    if (!entryFile.exists()) {                        entryFile.getParentFile().mkdirs();                    }                    byte[] buffer = new byte[1024 * 8];                    outputStream = new FileOutputStream(entryFile);                    int len = -1;                    while ((len = zipArchiveInputStream.read(buffer)) != -1) {                        outputStream.write(buffer, 0, len);                    }                    outputStream.flush();                    String[] pathArr = FastDFSUtils.uploadFile(entryFile, archiveEntryFileName);                    String filePath = pathArr[0] + File.separator + pathArr[1];                    fileInfoMap.put(archiveEntryFileName, filePath);                    outputStream.close();                }            }        } catch (IOException e) {            LOGGER.error("IOException:{}", ExceptionUtils.getFullStackTrace(e));            throw e;        } finally {            try {                if (null != outputStream) {                    outputStream.close();                }                if (null != zipArchiveInputStream) {                    zipArchiveInputStream.close();                }            } catch (IOException e) {                LOGGER.error("IOException:{}", ExceptionUtils.getFullStackTrace(e));                throw e;            }        }        return fileInfoMap;    }    /**     * 创建临时文件     */    public static File createTempFile(String fullFileName) throws IOException {        String tempDirectoryPath = FileUtils.getTempDirectoryPath();        File file = new File(tempDirectoryPath + File.separator + fullFileName);        file.deleteOnExit();        boolean newFile = file.createNewFile();        LOGGER.debug("newFile {} => {}", fullFileName, newFile);        return file;    }    /**     * 构建目录     *     * @param outputDir     * @param subDir     */    public static void createDirectory(String outputDir, String subDir) {        File file = new File(outputDir);        if (!Strings.isNullOrEmpty(subDir)) {            file = new File(outputDir + File.separator + subDir);        }        if (!file.exists()) {            if (!file.getParentFile().exists()) {                file.getParentFile().mkdirs();            }            file.mkdirs();        }    }}
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章