import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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 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;
}
}

JAVA对字符串的压缩与解压缩的更多相关文章

  1. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

  2. 利用JAVA API函数实现数据的压缩与解压缩

      综述 许多信息资料都或多或少的包含一些多余的数据.通常会导致在客户端与服务器之间,应用程序与计算机之间极大的数据传输量.最常见的解决数据存储和信息传送的方法是安装额外的存储设备和扩展现有的通讯能力 ...

  3. Java 基础【12】 压缩与解压缩

    Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...

  4. Java 基础【15】 压缩与解压缩

    Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...

  5. 利用SharpZipLib进行字符串的压缩和解压缩

    http://www.izhangheng.com/sharpziplib-string-compression-decompression/ 今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用 ...

  6. Java用Zip进行压缩

    这个总结源于Java编程思想第四版18.11节的案例: 完整代码地址: Java编程思想:压缩 相关Api地址: ZipStream ZipEntry ZipFile 进行压缩时: 1.创建Check ...

  7. Linux系统之压缩、解压缩,vi编辑器,系统初始化服务和系统监控

    一.正文处理,压缩与解压缩 1.内容重定向>与>> >:覆盖,将>号左边的结果覆盖到>号右边的文件中,如果文件不存在,则先创建一个新的空文件并覆盖 >> ...

  8. zip格式压缩、解压缩(C#)

    压缩方法 #region 压缩 /// <summary> /// 压缩 /// </summary> /// <param name="bytes" ...

  9. Deflater与Inflater的压缩与解压缩

    原文:Deflater与Inflater的压缩与解压缩 package util; import java.util.Arrays; import java.util.zip.Deflater; im ...

随机推荐

  1. 5.用 CSS 创作一个立体滑动 toggle 交互控件

    原文地址:https://segmentfault.com/a/1190000014638655 HTML代码: <html> <head> <link rel=&quo ...

  2. 31. Studio获取新的ID值方法

    var fun = ABS_LOADBEAN("com.plug.FunctionHelper");var vid1 = fun.utilHelper.getNextID(&quo ...

  3. sssp maven pom

    pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.or ...

  4. leetcode166

    public class Solution { public String fractionToDecimal(int numerator, int denominator) { HashMap< ...

  5. 1.Spring AOP (上)

    转自:https://blog.csdn.net/yusheng8711/article/details/21622773 Spring有两大核心,IOC和AOP.IOC在java web项目中无时无 ...

  6. Spring Boot学习--项目启动时执行特定方法

    Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动 ...

  7. AS3 localToGlobal、globalToLocal方法的总结

    (1).localToGlobal (point)   把point看成在(1)内部,计算出该point相当于stage的坐标 (2).globalToLocal (point)   point为全局 ...

  8. C#--抽象工厂设计模式原理

    C#--抽象工厂设计模式原理 C#--抽象工厂设计模式--三层框架 C#--使用反射改进简单工厂

  9. mongodb基础学习7-备份与恢复

    下面来讲讲mongodb的备份与恢复 备份可以备份为二进制格式,如果是用于数据交换,可以备份成json或cvs格式 导入/导出可以操作的是本地的mongodb服务器,也可以是远程的. 所以,都有如下通 ...

  10. mysqldump之不老将

    –add-drop-database 每个数据库创建之前添加drop数据库语句.mysqldump -uroot -p –all-databases –add-drop-database –add-d ...