/** * 加密,把一个字符串在原有的基础上+1 * @param data 需要解密的原字符串 * @return 返回解密后的新字符串 */ public static String encode(String data) { //把字符串转为字节数组 byte[] b = data.getBytes(); //遍历 for(int i=0;i<b.length;i++) { b[i] += 1;//在原有的基础上+1 } return new String(b); } /** * 解密:把一个加密后的字符串在原有基础上-1 * @param data 加密后的字符串 * @return 返回解密后的新字符串 */ public static String decode(String data) { //把字符串转为字节数组 byte[] b = data.getBytes(); //遍历 for(int i=0;i<b.length;i++) { b[i] -= 1;//在原有的基础上-1 } return new String(b); } /** * 使用gzip压缩字符串 * @param str 要压缩的字符串 * @return */ public static String compress(String str) { if (str == null || str.length() == 0) { return str; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = null; try { gzip = new GZIPOutputStream(out); gzip.write(str.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()); } /** * 使用gzip解压缩 * @param compressedStr 压缩字符串 * @return */ public static String uncompress(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; } public static String apacheCompress(String str) { if (str == null || str.length() == 0) { return str; } return org.apache.commons.codec.binary.Base64.encodeBase64String(str.getBytes()); } /** * 使用org.apache.commons.codec.binary.Base64解压缩 * @param compressedStr 压缩字符串 * @return */ public static String apacheUncompress(String compressedStr) { if (compressedStr == null) { return null; } byte[] bytes = Base64.decodeBase64(compressedStr); return new String(bytes); } /** * 字符串的压缩 */ public static String GZIPCompress(String str) throws IOException { if (null == str || str.length() <= 0) { return str; } // 创建一个新的 byte 数组输出流 ByteArrayOutputStream out = new ByteArrayOutputStream(); // 使用默认缓冲区大小创建新的输出流 GZIPOutputStream gzip = new GZIPOutputStream(out); // 将 b.length 个字节写入此输出流 gzip.write(str.getBytes()); gzip.close(); // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串 System.out.println( URLEncoder.encode(out.toString("ISO-8859-1"))); return out.toString("ISO-8859-1"); } /** * 字符串的解压 */ public static String GZIPunCompress(String str) throws IOException { if (null == str || str.length() <= 0) { return str; } // 创建一个新的 byte 数组输出流 ByteArrayOutputStream out = new ByteArrayOutputStream(); // 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组 ByteArrayInputStream in = new ByteArrayInputStream(str .getBytes("ISO-8859-1")); // 使用默认缓冲区大小创建新的输入流 GZIPInputStream gzip = new GZIPInputStream(in); byte[] buffer = new byte[256]; int n = 0; while ((n = gzip.read(buffer)) >= 0) {// 将未压缩数据读入字节数组 // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte数组输出流 out.write(buffer, 0, n); } // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串 return out.toString("UTF-8"); } /** * 使用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(); System.out.println("Zip:"+out.toString("GBK")); 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 compressed 压缩后的文本 * @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 content = "video:a98663f5-741f-4587-beaa-3dca1a85a1c5.MP4;audio:a98663f5-741f-4587-beaa-3dca1a85a1c5.MP4"; // String apacheCompress = apacheCompress(content); // System.out.println("apacheCompress:"+apacheCompress); // String apacheUncompress = apacheUncompress(apacheCompress); // System.out.println("apacheUncompress:"+apacheUncompress); // String GZIPCompress = GZIPCompress(content); // System.out.println("GZIPCompress:"+GZIPCompress); // String GZIPunCompress = GZIPunCompress(GZIPCompress); // System.out.println("GZIPunCompress:"+GZIPunCompress); String ZIPCompress = zip(content); System.out.println("ZIPCompress:"+ZIPCompress); String GZIPunCompress = unzip(ZIPCompress); System.out.println("ZIPunCompress:"+GZIPunCompress); // String compress = compress(content); // System.out.println("compress:"+compress); // String uncompress = uncompress(compress); // System.out.println("uncompress:"+uncompress); // String encode = encode(content); // System.out.println("encode:"+encode); // String decode = decode(encode); // System.out.println("decode:"+decode); }