---
title: "JAVA版PCX图象的RLE算法压缩与解压缩"
author: "iammonster"
date: "2009-05-26T17:09:17Z"
tags: []
categories: ["非技术"]
slug: "java-pcx-rle"
draft: false
lastmod: "2009-05-26T17:11:47Z"
id: 396565
url: "https://jiajun.iteye.com/blog/396565"
---

丰富JE的博客，把上大学时候的一个算法，搬过来，大概是2007年07月写的

 

```
/*闲来无事，写个RLE程序玩玩*/ 
package com.homework.comperssion; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
public class RLE { 

public void compress(String source,String dis){//压缩 source源文件，dis目标文件 
  try { 
         FileInputStream in = new FileInputStream(source); 
         FileOutputStream out=new FileOutputStream(dis); 
         int next = 0; 
         int count=in.read();//取第一个字节 
         while ((next = in.read()) >= 0){//当文件没有结束时执行 
          int counter=1;//计重复的次数 
          if(count==next){//如果有相同的 
           counter++; 
           while(next==(count=in.read())){//计算重复的次数 
            counter++; 
           } 
           while(counter>=63){//重复次数大于63的情况 
            out.write(255);//63个（192+63） 
            out.write(next); 
            //System.out.println("大于63的情况"+(0xc0+63)+" "+count); 
            counter-=63;//减去处理的63个字节 
           } 
           if(counter>1){//处理剩下的字节 
            out.write(0xc0+counter); 
            out.write(next); 
            //System.out.println("重复剩余的"+(0xc0+counter)+" "+counter); 
           } 
           } 
          else{ 
           if(count<=0xc0){//不重复小于192的情况 
            //System.out.println(count); 
            out.write(count); 
            count=next; 
           } 
           else{//不重复大于192的情况 
            out.write(0xc1); 
            out.write(count); 
            count=next; 
            //System.out.println("0xc1的"+(0xc1)+count);             
           } 
          } 
         } 
         //处理最后一个字节 
         if(count<=0xc0){ 
          //System.out.println(count); 
          out.write(count); 
         } 
         else{ 
          out.write(0xc1); 
          out.write(count); 
          //System.out.println("0xc1的"+(0xc1)+count);             
         } 
         in.close();//关闭输入流 
         out.close();//关闭输出流 
     } catch (IOException e) { 
     } 
} 
public void decompress(String source,String dis){//解压缩  source源文件，dis目标文件 
  try{ 
    FileInputStream in = new FileInputStream(source); 
       FileOutputStream out=new FileOutputStream(dis); 
       int count=0; 
       while((count=in.read()) >=0){ 
        if(count==0xc1) 
         out.write(in.read()); 
        else if(count<=0xc0){ 
         out.write(count); 
        } 
        else if(count>0xc1){ 
         int next=in.read(); 
         for(int i=0;i<(count-0xc0);i++) 
          out.write(next); 
        } 
       } 
  }catch(Exception e){ 
   e.printStackTrace(); 
  } 
} 
public static void main(String[] args) { 
  // TODO Auto-generated method stub   
  new RLE().compress("E:\\123.BMP", "E:\\456.cjj");//压缩到456.cjj ，文件后缀名随便取 
  new RLE().decompress("E:\\456.cjj", "E:\\789.BMP");//从456.cjj 解压到789.BMP 
} 
} 
```

 

呵呵，两年前的作品……想想那时候和老婆混到一起没多久呢？想起来蛮怀恋的。