package com.fdkankan.reg;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

/**
    * DeflaterUtil 压缩字符串
    */
public class DeflaterUtil {
      /**
       * 压缩
       */
      public static String zipString(String unzipString) {

         //使用指定的压缩级别创建一个新的压缩器。
         Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
         //设置压缩输入数据。
         deflater.setInput(unzipString.getBytes());
         //当被调用时，表示压缩应该以输入缓冲区的当前内容结束。
         deflater.finish();

         final byte[] bytes = new byte[1024];
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);

         while (!deflater.finished()) {
            //压缩输入数据并用压缩数据填充指定的缓冲区。
            int length = deflater.deflate(bytes);
            outputStream.write(bytes, 0, length);
         }
         //关闭压缩器并丢弃任何未处理的输入。
         deflater.end();
         System.out.println(outputStream.toByteArray());
         return Base64.getEncoder().encodeToString(outputStream.toByteArray());
      }

      /**
       * 解压缩
       */
      public static String unzipString(String zipString) {
         byte[] decode = Base64.getMimeDecoder().decode(zipString);
         //创建一个新的解压缩器  https://www.yiibai.com/javazip/javazip_inflater.html

         Inflater inflater = new Inflater();
         //设置解压缩的输入数据。
         inflater.setInput(decode);
         final byte[] bytes = new byte[1024];
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
         try {
            //finished() 如果已到达压缩数据流的末尾，则返回true。
            while (!inflater.finished()) {
               //将字节解压缩到指定的缓冲区中。
               int length = inflater.inflate(bytes);
               outputStream.write(bytes, 0, length);
            }
         } catch (DataFormatException e) {
            e.printStackTrace();
            return null;
         } finally {
            //关闭解压缩器并丢弃任何未处理的输入。
            inflater.end();
         }

         return outputStream.toString();
      }
      public static byte[] compress(String data) throws IOException {
         byte[] input = data.getBytes("UTF-8");
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         Deflater compressor = new Deflater();
         compressor.setLevel(Deflater.BEST_COMPRESSION);
         compressor.setInput(input);
         compressor.finish();
         byte[] buffer = new byte[1024];
         while (!compressor.finished()) {
            int count = compressor.deflate(buffer);
            outputStream.write(buffer, 0, count);
         }
         outputStream.close();
         return outputStream.toByteArray();
      }

      public static String decompress(byte[] data) throws IOException {
         Inflater decompressor = new Inflater();
         decompressor.setInput(data);
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         InflaterInputStream inflaterInputStream = new InflaterInputStream(
                 new ByteArrayInputStream(data), decompressor);
         byte[] buffer = new byte[1024];
         int length;
         while ((length = inflaterInputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
         }
         outputStream.close();
         byte[] output = outputStream.toByteArray();
         return new String(output, "UTF-8");
      }
   public static void main(String[] args) throws IOException {
      String data  = "123123123123";
      System.out.println(data.length());
      String formattedString = DeflaterUtil.zipString(data);
      System.out.println(formattedString.length());
      System.out.println(formattedString);

      // 还原为字符串
      String restoredData = DeflaterUtil.unzipString(formattedString);
      System.out.println(restoredData.length());
      System.out.println(restoredData);
      byte[] compress = compress(data);


   }
}
