• java 字符串压缩长度并解压


    package com.wy.data.emaildata.util;
    
    import org.apache.commons.io.FileUtils;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;
    
    public class ZipUtils {
    
        /**
         * 使用gzip进行压缩
         */
        public static String gzip(String primStr) {
            if (primStr == null || primStr.length() == 0) {
                return primStr;
            }
    
            ByteArrayOutputStream out = new ByteArrayOutputStream();
    
            GZIPOutputStream gzip = null;
            try {
                gzip = new GZIPOutputStream(out);
                gzip.write(primStr.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (gzip != null) {
                    try {
                        gzip.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
    
            return new sun.misc.BASE64Encoder().encode(out.toByteArray());
        }
    
        /**
         * <p>Description:使用gzip进行解压缩</p>
         *
         * @param compressedStr
         * @return
         */
        public static String gunzip(String compressedStr) {
            if (compressedStr == null) {
                return null;
            }
    
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = null;
            GZIPInputStream ginzip = null;
            byte[] compressed = null;
            String decompressed = null;
            try {
                compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
                in = new ByteArrayInputStream(compressed);
                ginzip = new GZIPInputStream(in);
    
                byte[] buffer = new byte[1024];
                int offset = -1;
                while ((offset = ginzip.read(buffer)) != -1) {
                    out.write(buffer, 0, offset);
                }
                decompressed = out.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (ginzip != null) {
                    try {
                        ginzip.close();
                    } catch (IOException e) {
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                    }
                }
            }
    
            return decompressed;
        }
    
        /**
         * 使用zip进行压缩
         *
         * @param str 压缩前的文本
         * @return 返回压缩后的文本
         */
        public static final String zip(String str) {
            if (str == null)
                return null;
            byte[] compressed;
            ByteArrayOutputStream out = null;
            ZipOutputStream zout = null;
            String compressedStr = null;
            try {
                out = new ByteArrayOutputStream();
                zout = new ZipOutputStream(out);
                zout.putNextEntry(new ZipEntry("0"));
                zout.write(str.getBytes());
                zout.closeEntry();
                compressed = out.toByteArray();
                compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
            } catch (IOException e) {
                compressed = null;
            } finally {
                if (zout != null) {
                    try {
                        zout.close();
                    } catch (IOException e) {
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                    }
                }
            }
            return compressedStr;
        }
    
        /**
         * 使用zip进行解压缩
         *
         * @param compressedStr 压缩后的文本
         * @return 解压后的字符串
         */
        public static final String unzip(String compressedStr) {
            if (compressedStr == null) {
                return null;
            }
    
            ByteArrayOutputStream out = null;
            ByteArrayInputStream in = null;
            ZipInputStream zin = null;
            String decompressed = null;
            try {
                byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
                out = new ByteArrayOutputStream();
                in = new ByteArrayInputStream(compressed);
                zin = new ZipInputStream(in);
                zin.getNextEntry();
                byte[] buffer = new byte[1024];
                int offset = -1;
                while ((offset = zin.read(buffer)) != -1) {
                    out.write(buffer, 0, offset);
                }
                decompressed = out.toString();
            } catch (IOException e) {
                decompressed = null;
            } finally {
                if (zin != null) {
                    try {
                        zin.close();
                    } catch (IOException e) {
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                    }
                }
            }
            return decompressed;
        }
    
        public static void main(String[] args) throws IOException {
           String strOld=FileUtils.readFileToString(new File("D:/test.txt"),"utf-8");
            System.out.println(strOld.length());
            String gzip = zip(strOld);
            System.out.println(gzip.length());
        }
    }
  • 相关阅读:
    启用oracle 11g自己主动收集统计信息
    在java中,怎样跳出当前的多重循环?
    从编程的角度理解gradle脚本﹘﹘Android Studio脚本构建和编程[魅族Degao]
    【SpringMVC架构】SpringMVC入门实例,解析工作原理(二)
    实现icon和文字垂直居中的两种方法-(vertical-align and line-height)
    android发送get请求时报错
    KeyEvent 键码值
    Android-黑科技-微信抢红包必备软件
    Delicious Apples (hdu 5303 贪心+枚举)
    vim 插件配置博客记录
  • 原文地址:https://www.cnblogs.com/wangyang108/p/10685917.html
Copyright © 2020-2023  润新知