|
1 | 1 | package pro.tools.data.text; |
2 | 2 |
|
| 3 | +import pro.tools.constant.StrConst; |
| 4 | + |
3 | 5 | import java.io.ByteArrayInputStream; |
4 | 6 | import java.io.ByteArrayOutputStream; |
5 | 7 | import java.io.IOException; |
6 | 8 | import java.util.zip.GZIPInputStream; |
7 | 9 | import java.util.zip.GZIPOutputStream; |
8 | 10 |
|
9 | 11 | /** |
10 | | - * Module: ZipUtil.java |
11 | | - * Description: 对字符串的压缩及解压 |
12 | | - * Company: |
13 | | - * Author: pantp |
14 | | - * Date: May 6, 2012 |
| 12 | + * 对字符串的压缩及解压 |
| 13 | + * |
| 14 | + * @author SeanDragon |
15 | 15 | */ |
16 | 16 | public class ToolStrCompress { |
17 | 17 |
|
18 | | - public static void main(String[] args) throws IOException { |
19 | | - // 字符串超过一定的长度 |
20 | | - String str = "ABCdef123中文~!@#$%^&*()_+{};/1111111111111111111111111AAAAAAAAAAAJDLFJDLFJDLFJLDFFFFJEIIIIIIIIIIFJJJJJJJJJJJJALLLLLLLLLLLLLLLLLLLLLL" + |
21 | | - "LLppppppppppppppppppppppppppppppppppppppppp===========================------------------------------iiiiiiiiiiiiiiiiiiiiiii"; |
22 | | - str = "美国学者 Dime 和 Henman 为解决信息公开传送和密钥管理问题,提出一种新的密钥交换协议,允许在不安全的媒体上的通讯双方交换信息,安全地达成一致的密钥,这就是 “公开密钥系统”。与对称加密算法不同,非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。工作过程如下图所示,甲乙之间使用非对称加密的方式完成了重要信息的安全传输。非对称加密工作过程简要示意图非对称加密工作过程简要示意图美国学者 Dime 和 Henman 为解决信息公开传送和密钥管理问题,提出一种新的密钥交换协议,允许在不安全的媒体上的通讯双方交换信息,安全地达成一致的密钥,这就是 “公开密钥系统”。与对称加密算法不同,非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。工作过程如下图所示,甲乙之间使用非对称加密的方式完成了重要信息的安全传输。非对称加密工作过程简要示意图非对称加密工作过程简要示意图"; |
23 | | -// str = "1"; |
24 | | - |
25 | | - System.out.println("\n原始的字符串为------->" + str); |
26 | | - float len0 = str.length(); |
27 | | - System.out.println("原始的字符串长度为------->" + len0); |
28 | | - |
29 | | - String ys = compress(str); |
30 | | - System.out.println("\n压缩后的字符串为----->" + ys); |
31 | | - float len1 = ys.length(); |
32 | | - System.out.println("压缩后的字符串长度为----->" + len1); |
33 | | - |
34 | | - String jy = unCompress(ys); |
35 | | - System.out.println("\n解压缩后的字符串为--->" + jy); |
36 | | - System.out.println("解压缩后的字符串长度为--->" + jy.length()); |
37 | | - |
38 | | - System.out.println("\n压缩比例为" + len1 / len0); |
39 | | - |
40 | | - //判断 |
41 | | - if (str.equals(jy)) { |
42 | | - System.out.println("先压缩再解压以后字符串和原来的是一模一样的"); |
43 | | - } |
| 18 | + private ToolStrCompress() { |
| 19 | + throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
44 | 20 | } |
45 | 21 |
|
46 | 22 | /** |
47 | 23 | * 字符串的压缩 |
48 | 24 | * |
49 | 25 | * @param str 待压缩的字符串 |
| 26 | + * |
50 | 27 | * @return 返回压缩后的字符串 |
| 28 | + * |
51 | 29 | * @throws IOException |
52 | 30 | */ |
53 | 31 | public static String compress(String str) throws IOException { |
54 | 32 | if (null == str || str.length() <= 0) { |
55 | 33 | return str; |
56 | 34 | } |
57 | | - // 创建一个新的 byte 数组输出流 |
58 | | - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
59 | | - // 使用默认缓冲区大小创建新的输出流 |
60 | | - GZIPOutputStream gzip = new GZIPOutputStream(out); |
61 | | - // 将 b.length 个字节写入此输出流 |
62 | | - gzip.write(str.getBytes()); |
63 | | - gzip.close(); |
64 | | - // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串 |
65 | | - return out.toString("UTF-8"); |
| 35 | + try ( |
| 36 | + // 创建一个新的 byte 数组输出流 |
| 37 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 38 | + // 使用默认缓冲区大小创建新的输出流 |
| 39 | + GZIPOutputStream gzip = new GZIPOutputStream(out);) { |
| 40 | + // 将 b.length 个字节写入此输出流 |
| 41 | + gzip.write(str.getBytes()); |
| 42 | + // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串 |
| 43 | + return out.toString(StrConst.DEFAULT_CHARSET_NAME); |
| 44 | + } |
66 | 45 | } |
67 | 46 |
|
68 | 47 | /** |
69 | 48 | * 字符串的解压 |
70 | 49 | * |
71 | 50 | * @param str 对字符串解压 |
| 51 | + * |
72 | 52 | * @return 返回解压缩后的字符串 |
| 53 | + * |
73 | 54 | * @throws IOException |
74 | 55 | */ |
75 | 56 | public static String unCompress(String str) throws IOException { |
76 | 57 | if (null == str || str.length() <= 0) { |
77 | 58 | return str; |
78 | 59 | } |
79 | | - // 创建一个新的 byte 数组输出流 |
80 | | - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
81 | | - // 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组 |
82 | | - ByteArrayInputStream in = new ByteArrayInputStream(str |
83 | | - .getBytes("UTF-8")); |
84 | | - // 使用默认缓冲区大小创建新的输入流 |
85 | | - GZIPInputStream gzip = new GZIPInputStream(in); |
86 | | - byte[] buffer = new byte[256]; |
87 | | - int n = 0; |
88 | | - while ((n = gzip.read(buffer)) >= 0) {// 将未压缩数据读入字节数组 |
89 | | - // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte数组输出流 |
90 | | - out.write(buffer, 0, n); |
| 60 | + try ( |
| 61 | + // 创建一个新的 byte 数组输出流 |
| 62 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 63 | + // 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组 |
| 64 | + ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes(StrConst.DEFAULT_CHARSET_NAME)); |
| 65 | + // 使用默认缓冲区大小创建新的输入流 |
| 66 | + GZIPInputStream gzip = new GZIPInputStream(in);) { |
| 67 | + byte[] buffer = new byte[256]; |
| 68 | + int n = 0; |
| 69 | + // 将未压缩数据读入字节数组 |
| 70 | + while ((n = gzip.read(buffer)) >= 0) { |
| 71 | + // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte数组输出流 |
| 72 | + out.write(buffer, 0, n); |
| 73 | + } |
| 74 | + // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串 |
| 75 | + return out.toString(StrConst.DEFAULT_CHARSET_NAME); |
91 | 76 | } |
92 | | - // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串 |
93 | | - return out.toString("UTF-8"); |
94 | 77 | } |
95 | 78 |
|
96 | 79 | } |
0 commit comments