加密压缩+解压解密

public class ZipToBase64 {
 
    private static final int BUFFER_SIZE = 2 * 1024;
 
    private static final Logger log = LoggerFactory.getLogger(ZipToBase64.class);


    /**
     * 加压加密
     * @param srcFiles 需要压缩的文件
     * @return Base64 压缩文件后进行base64编码
     * @throws RuntimeException
     */
    public static String toBase64Zip(List<File> srcFiles) throws RuntimeException {

        log.info("开始压缩文件    [{}]", srcFiles);

        long start = System.currentTimeMillis();

        String base64toZip = "";

        ZipOutputStream zos = null;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {

            zos = new ZipOutputStream(baos);

            for (File srcFile : srcFiles) {

                byte[] buf = new byte[BUFFER_SIZE];
                log.info("压缩的文件名称    [{}]   ", srcFile.getName() + "压缩的文件大小      [{}] ", srcFile.length());

                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;

                FileInputStream in = new FileInputStream(srcFile);

                while ((len = in.read(buf)) != -1) {

                    zos.write(buf, 0, len);
                }

                zos.closeEntry();

                in.close();
            }
            long end = System.currentTimeMillis();
            log.info("压缩完成,耗时:    [{}] ms", (end - start));

        } catch (Exception e) {

            throw new RuntimeException("zip error from ZipToBase64", e);
        } finally {

            if (zos != null) {

                try {

                    zos.close();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
        }
        //加密
        byte[] refereeFileBase64Bytes =Base64.getEncoder().encode(baos.toByteArray());

        try {

            base64toZip = new String(refereeFileBase64Bytes, "UTF-8");

        } catch (Exception e) {

            throw new RuntimeException("压缩流出现异常", e);
        }

        return base64toZip;
    }

 
    /**
     * 解密解压
     * <p>
     * Title: ZipToBase64.java</p>
     * <p>
     * Description: Base64 解密 zip 解压缩 </p>
     * 
     * @param path   解压文件夹路径 文件夹父文件可更改
     * @param base64 base64加密字符
     * @return Base64 压缩文件后进行base64编码
     * @version 1.0
     */
    public static void Base64ToFile(String base64, String path) throws RuntimeException {

        log.info("解压文件地址" + path);

        try {

            byte[] byteBase64 =Base64.getDecoder().decode(base64);
            ByteArrayInputStream byteArray = new ByteArrayInputStream(byteBase64);
            ZipInputStream zipInput = new ZipInputStream(byteArray);
            ZipEntry entry = zipInput.getNextEntry();
            File fout = null;
            File file = new File(path);
            String parent = file.getParent();

            while (entry != null && !entry.isDirectory()) {

                log.info("文件名称:    [{}]", entry.getName());
                fout = new File(parent, entry.getName());
                if (!fout.exists()) {
                    (new File(fout.getParent())).mkdirs();
                }
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fout));
                int offo = -1;
                byte[] buffer = new byte[BUFFER_SIZE];
                while ((offo = zipInput.read(buffer)) != -1) {
                    bos.write(buffer, 0, offo);
                }
                bos.close();
                // 获取 下一个文件
                entry = zipInput.getNextEntry();
            }
            zipInput.close();
            byteArray.close();

        } catch (Exception e) {

            throw new RuntimeException("解压流出现异常", e);

        }
 
    }
 
    public static void main(String[] args) {
        // 文件压缩
        List<File> fileList = new ArrayList<File>();

        fileList.add(new File("D:\\aa.xlsx"));
        fileList.add(new File("D:\\wlyd_cbs.sql"));
        fileList.add(new File("D:\\p1.jpg"));

        //加压加密
        String base64 = ZipToBase64.toBase64Zip(fileList);

        log.info("文件base 64 加密:" + base64);

        //解压解密
        ZipToBase64.Base64ToFile(base64, "D:\\bb\\all.zip");
 
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值