Java--数据的压缩
3、数据的压缩
GzipOutputStream压缩工具流。
- > > ByteArrayOutputStream压缩容器。
。
1:以下是在某个servlet中对指定的数据进行压缩:
.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class GzipServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
//声明准备被压缩的数据
String str = "Hello你好Hello你好在内存中声明一Hello你好在" +
"内存中声明一个Hello你好在内存中声明一个Hello你" +
"好在内存中声明一个<br/>容器声明准备被压缩获取准备被压缩" +
"的数据的字节码的数据容器声明准备被压缩获取准备被压缩的数" +
"据的字节码的数据容器声明准备被压缩获取准备被压缩的数据的" +
"字节码的数据个容器声明准备被压缩获取准备被压缩的数据的字节码的" +
"数据在内存中声明一个容器声明准备被压缩获取准备被压缩的数据" +
"的字节码的数据";
//2:获取准备被压缩的数据的字节码
byte[] src = ("UTF-8");
//3:在内存中声明一个容器
ByteArrayOutputStream destByte = new ByteArrayOutputStream();
//4:声明压缩的工具流,并设置压缩的目的地为destByte
GZIPOutputStream zip = new GZIPOutputStream(destByte);
//5:写入数据
(src);
//6:关闭压缩工具流
();
("压缩之前字节码大小:"+);
//7:获取压缩以后数据
byte[] dest = ();
("压缩以后的字节码大小:"+);
Java--数据的压缩 来自淘豆网www.taodocs.com转载请标明出处.