public class Compress extends JPanel{
String DataName ;
Compress(){
this.setSize(300,300);
this.setLayout(null);
JButton Button_Choose = new JButton("選擇");
JButton Button_Compress = new JButton("壓縮");
JTextField DataSite = new JTextField();
Button_Choose.setSize(60,25);
Button_Compress.setSize(60,25);
DataSite.setSize(200,25);
Button_Choose.setLocation(215, 100);
Button_Compress.setLocation(110, 135);
DataSite.setLocation(10, 100);
DataSite.setText("請選擇檔案");
this.add(Button_Choose);
this.add(Button_Compress);
this.add(DataSite);
HashMap <String, Integer> AppearTimes = new HashMap <String, Integer>();
Button_Choose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
JFileChooser FileChooser =new JFileChooser();
int ret = FileChooser.showOpenDialog(null);
if( ret == JFileChooser.APPROVE_OPTION)
{
DataName = FileChooser.getSelectedFile().getName() ;
DataSite.setText(FileChooser.getCurrentDirectory()+"\\"+FileChooser.getSelectedFile().getName());
}
} catch (Exception e2){
JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.ERROR_MESSAGE);
}
}
});
Button_Compress.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
FileReader fr;
try {
fr = new FileReader(DataSite.getText());
BufferedReader br = new BufferedReader(fr);
JOptionPane.showMessageDialog(null,"開始壓縮檔案!");
while (br.ready())
{
String InputLine = br.readLine();
for ( int i = 0 ; i < InputLine.length() ; i ++ )
{
try
{
AppearTimes.put(""+InputLine.charAt(i),AppearTimes.get(InputLine.charAt(i))+1) ;
} catch ( NullPointerException e2 )
{
AppearTimes.put(""+InputLine.charAt(i),1) ;
}
}
try
{
AppearTimes.put("\\r\\n",AppearTimes.get("\r\n")+1) ;
} catch ( NullPointerException e2 )
{
AppearTimes.put("\\r\\n",1) ;
}
}
fr.close();
Vector <Node> v = new Vector();
Iterator iter = AppearTimes.entrySet().iterator();
Node n , m , CombineNode ;
while (iter.hasNext())
{
HashMap.Entry entry = (HashMap.Entry) iter.next();
n = new Node();
n.key = (String) entry.getKey();
n.value = (int) entry.getValue();
v.add(n);
}
PriorityQueue PQ = new PriorityQueue();
PQ.insert(v);
while( PQ.heap.size() > 1 )
{
n = PQ.remove() ;
m = PQ.remove() ;
CombineNode = new Node();
CombineNode.key = null ;
CombineNode.value = n.value + m.value ;
CombineNode.left = n ;
CombineNode.right = m ;
v.clear();
v.add(CombineNode) ;
PQ.insert(v);
}
Node TreeNode = PQ.remove() ;
HashMap <String, String> HuffmanCode = new HashMap <String, String>();
if ( TreeNode.left == null && TreeNode.right == null )
{
TreeNode.code = "0" ;
}
else
{
Queue <Node> q = new LinkedList();
q.offer(TreeNode);
while (!q.isEmpty())
{
Node p = q.poll();
if ( p.key != null)
HuffmanCode.put(p.key,p.code) ;
if (!(p.left == null) )
{
q.offer(p.left);
p.left.code = p.code + "0" ;
}
if (!(p.right == null))
{
q.offer(p.right);
p.right.code = p.code + "1" ;
}
}
}
FileWriter fw = new FileWriter("C_"+DataName);
fr = new FileReader(DataSite.getText());
br = new BufferedReader(fr);
iter = HuffmanCode.entrySet().iterator();
while (iter.hasNext())
{
HashMap.Entry entry = (HashMap.Entry) iter.next();
String c = (String) entry.getKey() ;
String s = (String) entry.getValue() ;
for ( int k = 0 ; k < c.length() ; k ++ )
{
int ni = (int)c.charAt(k);
String ns = Integer.toBinaryString(ni) ;
fw.write(ns);
}
fw.write(Integer.toBinaryString(32)) ;
fw.write(s);
fw.write(Integer.toBinaryString(32)) ;
}
fw.write(HuffmanCode.get("\\r\\n"));
while (br.ready())
{
String InputLine = br.readLine();
for ( int i = 0 ; i < InputLine.length() ; i ++ )
{
fw.write(HuffmanCode.get(""+InputLine.charAt(i)));
}
fw.write(HuffmanCode.get("\\r\\n"));
}
fw.flush();
fw.close();
BufferedOutputStream fw2 = new BufferedOutputStream(new FileOutputStream("C_"+DataName+".cpr"));
fr = new FileReader("C_"+DataName);
br = new BufferedReader(fr);
int time = 0 ;
while (br.ready())
{
String InputLine = br.readLine();
InputLine += HuffmanCode.get("\\r\\n");
if ( InputLine.length() % 7 != 0 )
{
for ( int i = InputLine.length() % 7 ; i < 8 ; i ++ )
InputLine += "0" ;
}
byte[] buf = new byte[InputLine.length() / 7];
for(int i = 0; i * 7 + 7 <= InputLine.length(); i ++)
buf[i] = Byte.valueOf(InputLine.substring(i * 7, i * 7 + 7), 2);
fw2.write(buf);
}
iter = HuffmanCode.entrySet().iterator();
while (iter.hasNext())
{
HashMap.Entry entry = (HashMap.Entry) iter.next();
System.out.println(entry.getKey()+" "+entry.getValue() ) ;
}
fw2.flush();
fw2.close();
JOptionPane.showMessageDialog(null,"壓縮完成!");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.ERROR_MESSAGE);
}
}
});
}