java压缩和解压 减少带宽

目录

1 Java 压缩和解压

1.zip是一种较为常见的压缩格式,Java提供了java.util.zip包,常用类:

2.Jar是Java专有的一种压缩格式,Java提供了java.util.jar包,常用类:

3.GZIP是Unix系统的文件压缩格式,Linux系统中经常用的.gz 文件就是GZIP格式,Java提供了

解码

 


1 Java 压缩和解压

为了减少传输时的数据量 在Java中提供了专门的压缩流将文件或者文件夹压缩成zip,gzip,jar等文件形式。

压缩流实现

Java支持的三种压缩格式:zip、jar、gzip。 不通文本压缩率不同

 public static void main(String[] args) throws IOException {
        String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa陈振东,陈振东,陈振东";
        System.out.println("字符串长度:"+s.length());
        //1 先压缩-->再转成byte数组
         byte[] compress = compress(s);
        System.out.println("压缩后::"+compress(s).length);
        System.out.println(Arrays.toString(compress));
        //2 解压成 byte数组
        byte[] uncompress = uncompress(compress);
        System.out.println("解压后:"+uncompress(compress(s)).length);
        System.out.println(Arrays.toString(uncompress));
        //3 解压转成字符串
        String uncompressToString = uncompressToString(compress(s));
        System.out.println("解压字符串后::"+uncompressToString(compress(s)).length());
        System.out.println(uncompressToString);
    }

1.zip是一种较为常见的压缩格式,Java提供了java.util.zip包,常用类:

ZipInputStream

ZipOutputStream

ZipEntry

ZipFile

依靠上述4个类完成zip类型文件操作。

	/**
	 * 使用zip进行压缩
	 * @date 2019年2月20日
	 * @author 陈振东
	 * @param str压缩前的文本
	 * @return 返回压缩后的文本
	 */
	@SuppressWarnings("restriction")
	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进行解压缩
	 * @date 2019年2月20日
	 * @author 陈振东
	 * @param compressedStr  压缩后的文本
	 * @return 解压后的字符串
	 */
	@SuppressWarnings("restriction")
	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;
	}

2.Jar是Java专有的一种压缩格式,Java提供了java.util.jar包,常用类:

JarInputStream

JarOutputStream

JarEntry

JarFile

 

3.GZIP是Unix系统的文件压缩格式,Linux系统中经常用的.gz 文件就是GZIP格式,Java提供了

GZipInputStream

GZipOutputStream

下面以Gzip为例

编码

/**
	 * @date 2019年2月20日
	 * @author 陈振东
	 * @param primStr
	 * @return
	 */
	@SuppressWarnings("restriction")
	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();
				}
			}
		}
//		byte[] bts = out.toByteArray();
//		return Arrays.toString(bts);
		return new sun.misc.BASE64Encoder().encode(out.toByteArray());
	}

解码

/**
	 * @date 2019年2月20日
	 * @author 陈振东
	 * @param compressedStr
	 * @return
	 */
	@SuppressWarnings("restriction")
	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;
	}

 

发布了38 篇原创文章 · 获赞 26 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/sdrfengmi/article/details/87795429