字符串压缩类

字符串压缩与解压的类

import java.io.ByteArrayInputStream;   
import java.io.ByteArrayOutputStream;   
import java.io.IOException;   
import java.util.zip.GZIPInputStream;   
import java.util.zip.GZIPOutputStream; 
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.File;
 
// 将一个字符串按照zip方式压缩和解压缩   
public class Zip {   
  
    // 压缩
    public static String compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes("UTF-8"));
        gzip.close();

        // 压缩后的内容保存到 dump.dat 文件,并以文本形式保存相应的数组到 array.txt
        // 将 array.txt 中的内容复制到 ArrayStr.java 中即可在代码中得到数组解压后的字符串
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("dump.dat");
            fileOutputStream.write(out.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte ArrayByte[] = out.toByteArray();
        System.out.println(Integer.toString(ArrayByte.length));
        int j = 0;
        String line = "";
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("array.txt"));
            bw.write("public static final byte[] data = {");
            bw.newLine();
            for (int i=0; i<ArrayByte.length; i++) {
                
                line += String.format("(byte)0x%02x, ", ArrayByte[i]);
                //line += String.format("%03d, ", ArrayByte[i]);
                j++;
                if (j == 32 || i == ArrayByte.length - 1) {
                    bw.write(line);
                    bw.newLine();
                    j = 0;
                    line = "";
                }
            }
            bw.write("};");
            bw.newLine();
            bw.write("String strCompressed = new String(data);");
            bw.newLine();
            bw.close();
        } catch (Exception e) {
            System.out.println("Error");
            e.printStackTrace();
        }
            

        return out.toString("ISO-8859-1");
    }

    // 解压缩
    public static String uncompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        // toString()使用平台默认编码,也可以显式的指定如toString("GBK")
        return out.toString("UTF-8");
    }
 
    public static String readFileContent(String fileName) {
        String FileContent = "";
        try {
            FileInputStream fis = new FileInputStream(fileName);   
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");   
            BufferedReader br = new BufferedReader(isr);   
            String line = "";   
            while ((line = br.readLine()) != null) {   
                FileContent += line;   
                //FileContent += "\r\n";
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return FileContent;
    }
  

  
    // 测试方法   
    public static void main(String[] args) throws IOException {   
            
    String str = Zip.readFileContent("js_packed.txt");
    //System.out.println(ZipUtil2.compress(str));
    System.out.println("原长度:" + str.length());     
    System.out.println("压缩后:" + Zip.compress(str).length());     
    System.out.println("解压缩:" + Zip.uncompress(Zip.compress(str)));   
    }   
}  

上面的类中可以在压缩过程中将压缩的字符串以数组形式导出到 array.txt 和 dump.dat 中,以下类使用数组保存压缩后的字符串,并将其解压得到原来的字符串

import java.io.ByteArrayInputStream;   
import java.io.ByteArrayOutputStream;   
import java.io.IOException;   
import java.util.zip.GZIPInputStream;   
 
// 将一个字符串按照zip方式压缩和解压缩   
public class ArrayStr {   
    public static final byte[] data = {
		// 将 array.txt 中的数据粘贴到此处
        (byte)0x1f, (byte)0x8b, (byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00, ...

        };
          
    // 解压缩
    public static String uncompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        // toString()使用平台默认编码,也可以显式的指定如toString("GBK")
        return out.toString("UTF-8");
    }
	
	// 将数组解压得到原字符串
    public static String getArrayStr(){
        String ret = "";
        String strCompressed = "";

        try {
            strCompressed = new String(data, "ISO-8859-1");
            return uncompress(strCompressed);  
        } catch (Exception e) {
            //TODO: handle exception
        }
        
        try {
            ret = uncompress(strCompressed);           
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
        return ret;            
    }
  

  
    // 测试方法   
    public static void main(String[] args) throws IOException {  
        System.out.println("解压缩:" + ArrayStr.getArrayStr());   
    }   
 
}  
posted @ 2022-03-02 23:34  汉学  阅读(35)  评论(0)    收藏  举报