nanchen2251 / CompressHelper

:fire: 压缩文件,压缩图片,压缩Bitmap,Compress, CompressImage, CompressFile, CompressBitmap:https://github.com/nanchen2251/AiYaCompressHelper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

怎样压缩文件为.zip ,怎样解压?

TYUpya opened this issue · comments

怎样压缩文件为.zip ,怎样解压?

package com.jrx.srvapi.web.FileDownLoad.utils;

import java.io.*;
import java.util.zip.GZIPOutputStream;

/**

  • @aothor tianwenkai

  • @DaTa 2018/04/26

  • DESC:文件压缩工具类
    */
    public class ZIPUtil {

    public static final int BUFFER = 1024;
    public static final String EXT = ".zip";

    /**

    • 文件压缩

    • @param file

    • @param delete 是否删除原始文件

    • @throws Exception
      */
      public static byte[] compress(File file, boolean delete) throws Exception {

      FileInputStream fis = new FileInputStream(file);
      BufferedInputStream inBuffer = new BufferedInputStream(fis);

      FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);
      BufferedOutputStream outBuffer = new BufferedOutputStream(fos);

      byte[] data = compress(inBuffer, outBuffer);
      fis.close();
      fos.flush();
      fos.close();
      if (delete) {
      file.delete();
      }
      return data;
      }

    /**

    • 文件压缩
    • @param file
    • @throws Exception
      */
      public static void compress(File file) throws Exception {
      compress(file, false);
      }

    /**

    • 数据压缩
    • @param is
    • @param os
    • @throws Exception
      */
      public static byte[] compress(InputStream is, OutputStream os)
      throws Exception {
      GZIPOutputStream gos = new GZIPOutputStream(os);
      int count;
      byte data[] = new byte[BUFFER];
      while ((count = is.read(data, 0, BUFFER)) != -1) {
      gos.write(data, 0, count);
      }
      gos.finish();
      gos.flush();
      gos.close();
      return data;
      }

}

只要有jar包就可以了 都是工具类 还可以优化的 希望多多提出宝贵的意见