压缩一个字符串,然后在把他解压,代码实现,怎么写,帮忙啊!

解决方案 »

  1.   


    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.   

    谢谢你 啊,
    我是想把String压缩后,去用这个压缩后的String,用完后,在把它解压。