.NETプロジェクトJAVA再構築の圧縮&解凍


第一部分:JAVA文字列圧縮と解凍実現
/*       */
public static String Compress(String input) throws IOException {   
    if (input == null || input.length() == 0) {   
    return input;   
    }   
    ByteArrayOutputStream out = new ByteArrayOutputStream();   
    GZIPOutputStream gzip = new GZIPOutputStream(out);   
    gzip.write(input.getBytes());
    gzip.close();   
    return out.toString("ISO-8859-1"); 
    }  


/*       */
public static String Decompress(String input) throws IOException {   
    if (input == null || input.length() == 0) {   
    return input;   
    }   
    ByteArrayOutputStream out = new ByteArrayOutputStream();   
    ByteArrayInputStream in = new ByteArrayInputStream(
    input.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();   
    }   

第2部:バイト配列圧縮&解凍
/* byte  GZip   */
public static byte[] Compress(byte[] data) {  
         byte[] res = null;  
         try {  
         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
         GZIPOutputStream gzip = new GZIPOutputStream(baos);  
         gzip.write(data);  
         gzip.finish();  
         gzip.close();  
         res = bos.toByteArray();  
         bos.close();  
         } catch (Exception ex) {  
         ex.printStackTrace();  
         }  
         return res;  
         }  



/* byte  GZip    */
public static byte[] Decompress(byte[] data) {  
        byte[] res = null;  
        try {  
        ByteArrayInputStream bais = new ByteArrayInputStream(data);  
        GZIPInputStream gzip = new GZIPInputStream(bais);  
        byte[] buf = new byte[1024];  
        int num = -1;  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {  
        baos.write(buf, 0, num);  
        }  
        res = baos.toByteArray();  
        baos.flush();  
        baos.close();  
        gzip.close();  
        bais.close();  
        } catch (Exception ex) {  
        ex.printStackTrace();  
        }  
        return res;  
        }  

注:以上は実装コード、具体的なクラスと関連原理であり、詳しく説明する機会がある.