美文网首页
java对字符串进行压缩

java对字符串进行压缩

作者: Nick_4438 | 来源:发表于2022-07-21 22:30 被阅读0次

话不多说直接上代码


package com.example.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.*;

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");
        String strOld ="https://blog.csdn.net/RaoSK/article/details/121519355,https://blog.csdn.net/RaoSK/article/details/121519355,https://blog.csdn.net/RaoSK/article/details/121519355,https://blog.csdn.net/RaoSK/article/details/121519355,https://blog.csdn.net/RaoSK/article/details/121519355,";
        String gzip = zip(strOld);
        System.out.printf("old:=%d zip new:%d\r\n",strOld.length(),gzip.length());
        System.out.printf("new:=%s ",gzip);

        System.out.printf("\r\n...... ",gzip);
        String strOld1 ="www.baidu.com1234567890abcdefeaghodadfsdfasdf.sdf.sadfmsmadmfmsdmfmasmdfsdkfkaskdfsdfmamwefkksafs.df..asd.f.asdfwkefkkejuuwq@##&&**!^&!*!((!))!))!)!))!)!(*&^%%$%&!*!***!*!*!*!**!*!*!*!**!******!&&^^JJSSYDHDJoadsp;fddlslklsdlkflksdlwww.baidu.com1234567890abcdefeaghodadfsdfasdf.sdf.sadfmsmadmfmsdmfmasmdfsdkfkaskdfsdfmamwefkksafs.df..asd.f.asdfwkefkkejuuwq@##&&**!^&!*!((!))!))!)!))!)!(*&^%%$%&!*!***!*!*!*!**!*!*!*!**!******!&&^^JJSSYDHDJoadsp;fddlslklsdlkflksdlwww.baidu.com1234567890abcdefeaghodadfsdfasdf.sdf.sadfmsmadmfmsdmfmasmdfsdkfkaskdfsdfmamwefkksafs.df..asd.f.asdfwkefkkejuuwq@##&&**!^&!*!((!))!))!)!))!)!(*&^%%$%&!*!***!*!*!*!**!*!*!*!**!******!&&^^JJSSYDHDJoadsp;fddlslklsdlkflksdlwww.baidu.com1234567890abcdefeaghodadfsdfasdf.sdf.sadfmsmadmfmsdmfmasmdfsdkfkaskdfsdfmamwefkksafs.df..asd.f.asdfwkefkkejuuwq@##&&**!^&!*!((!))!))!)!))!)!(*&^%%$%&!*!***!*!*!*!**!*!*!*!**!******!&&^^JJSSYDHDJoadsp;fddlslklsdlkflksdl";
        String gzip1 = zip(strOld1);
        System.out.printf("old:=%d zip new:%d\r\n",strOld1.length(),gzip1.length());
        System.out.printf("new:=%s ",gzip1);

    }
}

  • 测试结果
old:=270 zip new:146
new:=UEsDBBQACAgIAMSz9VQAAAAAAAAAAAAAAAABAAAAMMsoKSkottLXT8rJT9dLLk7J08tLLdEPSswP
9tZPLCrJTM5J1U9JLUnMzCnWNzQyNDW0NDY11ckYproAUEsHCB9dHA87AAAADgEAAA==
 
...... old:=924 zip new:300
new:=UEsDBBQACAgIAMSz9VQAAAAAAAAAAAAAAAABAAAAMO1Puw7CMAz8lqg0Khki3g+xMHRAXTuxVDK4
5uFEBVlVfp+kMPALSNi+sy2fhwsh2BPcsLfnzk9n88Vytd5sJ3A6Y0stXK4dApIgQYQdEHcvHtDH
lgjEJwUTg/Cg9eBDS8wCJDa+2PhsKTEFTof23vfhuc8yrY1RjVZGFYUaj4d6U2F0k+ejPN1MFH3y
exhCad00VVXXx/JQVh2gPHaE6MSxE3RMjmMLf5t/m79m8wVQSwcIBLLDZq8AAACcAwAA
 
Process finished with exit code 0

补充

该程序使用的zip压缩算法,具体参考文章

相关文章

  • java对字符串进行压缩

    话不多说直接上代码 测试结果 补充 该程序使用的zip压缩算法,具体参考文章[https://www.ngui.c...

  • iOS 字符串压缩和解压缩

    实现对字符串进行压缩和解压缩操作 前提需要导入libz.1.2.5.dylib 库 import "BYView...

  • Java字符串压缩

    java 压缩字符串 如果源字符串长度小于64,压缩后的字符会比源字符串长。例如:str.length()=32c...

  • 2018-03-20 Json数据压缩

    使用GZIP对Json进行压缩与解压缩,主要用到java.util.zip.GZIPInputStream和jav...

  • Android App 之ProGuard混淆

    ProGuard原理 ProGuard能够对Java类中的代码进行压缩(Shrink),优化(Optimize),...

  • Java图片压缩处理

    网页中某些图片大的时候,图片会加载慢,这时候需要对图片进行压缩处理,Java有对图片进行压缩处理的方法。 整体思路...

  • 压缩算法进行字符串压缩

    1.使用Deflater压缩json,Inflater解压json Deflater 是同时使用了LZ77算法与哈...

  • Proguard首次学习

    参考 简介 可以对java类文件(不只是java语言生成的java类文件奥),进行压缩,优化,混淆,预校验。 压缩...

  • Java8

    对List中的字符串进行操作并返回 使用Java8之前 使用Java8后

  • ProGuard

    Java代码混淆工具——ProGuard 1.ProGuard简介 简单来说,ProGuard是一个对代码进行压缩...

网友评论

      本文标题:java对字符串进行压缩

      本文链接:https://www.haomeiwen.com/subject/qgrnurtx.html