java中进行压缩与解压

1.使用gzip进行压缩与解压

(1)压缩

public static String gzip(String primStr) {
        //如果所要压缩的数据为空或者长度为零,返回它
		if (primStr == null || primStr.length() == 0) {
			return primStr;
		}

        //字节数组输出流,在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组 缓冲区中。解压缩通常用于此流
		ByteArrayOutputStream out = new ByteArrayOutputStream();
        //gzip的输出流(压缩)
		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 out.toString();
	}

 (2)解压

public static String gunzip(String compressedStr) {
        //如果要解压的数据为null,返回null
		if (compressedStr == null) {
			return null;
		}
        //声明一个字符数组输出流,用于写入
		ByteArrayOutputStream out = new ByteArrayOutputStream();
        //声明一个字符数组输入流,用于解压
		ByteArrayInputStream in = null;
        //gzip的输入流(解压)
		GZIPInputStream ginzip = null;
		
		String decompressed = null;
		try {
			//将要进行解压的数据放入流里面
			in = new ByteArrayInputStream(compressedStr.getBytes());
            //代表将按照字节数组输入的方式进行解压
			ginzip = new GZIPInputStream(in);
            //声明每次读取和写入的大小,通常为1024
			byte[] buffer = new byte[1024];
			int offset = -1;//-1代表没数据
            //当每次读取的数据不为-1时,既有数据,将读入的数据写入到字符数组中,每次从0开始,长度为offest
			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;
	}

2.使用zip进行压缩与解压(同理)

(1)压缩

public static String zip(String str) {
		if (str == null)
			return null;
		byte[] compressed;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;

		try {
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);

            //设置ZipEntry对象,并对需要压缩的文件命名
			zout.putNextEntry(new ZipEntry("0"));

			zout.write(str.getBytes());
            //关闭当前ZIP条目并定位流以读取下一个条目,关闭ZipEntry,与putNextEntry对应
			zout.closeEntry();

			compressed = out.toString();
			
		} 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 compressed;
	}

(2)解压

public static final String unzip(String compressedStr) {
		if (compressedStr == null) {
			return null;
		}

		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		ZipInputStream zin = null;
		
		try {
			
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressedStr.toByteArray());
			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;
	}

ByteArrayOutputStream字节数组输出流,在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组 缓冲区中。解压缩通常用于此流。

ByteArrayOutputStream 对byte类型数据进行写入的类 相当于一个中间缓冲层,将类写入到文件等其他outputStream。可使用toByteArray() 和 toString() 获取数据。

ByteArrayInputStream同理。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值