JAVA ZIP压缩函数封装
/** * 压缩 * * @param str * @return * @throws IOException */ public static String zipCompress(String str){ if (str == null || str.length() == 0) { return ""; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = null; try { gzip = new GZIPOutputStream(out); gzip.write(str.getBytes("UTF-16LE")); } catch (IOException e) { e.printStackTrace(); } finally { if (gzip != null) { try { gzip.close(); } catch (IOException e) { e.printStackTrace(); } } } return Base64.encodeToString(out.toByteArray(),Base64.DEFAULT); }
C#封装方法
/// <summary>
/// 从原始字符串生成已压缩的字符串。
/// </summary>
/// <param name="stringToCompress">原始字符串。</param>
/// <returns>返回已压缩的字符串。</returns>
public static string CompressStr(string stringToCompress)
{
byte[] compressedData = CompressToByte(stringToCompress);
string strOut = Convert.ToBase64String(compressedData);
return strOut;
}
/// <summary>
/// 从原始字符串生成已压缩的字节数组。
/// </summary>
/// <param name="stringToCompress">原始字符串。</param>
/// <returns>返回已压缩的字节数组。</returns>
public static byte[] CompressToByte(string stringToCompress)
{
byte[] bytData = Encoding.Unicode.GetBytes(stringToCompress);
return Compress(bytData);
}