注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 你会划分和聚合子网吗?
 帮助

压缩一个字符串


2007-11-04 01:44:21
 标签:字符串 java 压缩   [推送到技术圈]

import java.util.zip.*;
import java.io.*;
import java.util.*;


public class ZipCompress {
 
public static void main(String[] args) throws IOException {
     FileOutputStream f
= new FileOutputStream("test.zip"); //把字符串写到test.zip的压缩文件中
     CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
     ZipOutputStream zos
= new ZipOutputStream(csum);
     BufferedOutputStream out
= new BufferedOutputStream(zos);
     zos.putNextEntry(
new ZipEntry("CompressedStrings.txt"));
    
byte[] stringBytes = "Here is the Strings would be Compressed".getBytes();
    
//stringBytes是要进行压缩的字符串
      out.write(stringBytes);
     out.flush();
     out.close();

   
//Reading Zip file
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi
= new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2
= new ZipInputStream(csumi);
    BufferedInputStream bis
= new BufferedInputStream(in2);
    ZipEntry ze;
   
while((ze = in2.getNextEntry()) != null) {
     
int x;
     
while((x = bis.read()) != -1)
        System.out.print((
char)x);
    }

  }
}





    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: