Tue Apr 17, 2007 11:07 am
package lzw_copmersion;
import lzw_copmersion.LZW_frame.myframe;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
public static void main(String[] args) {
// TODO code application logic here
new myframe();
}
}
package lzw_copmersion.LZW_frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class comPanel extends JPanel {
comPanel(JTextArea area) {
this.area=area;
setSize(550,50);
setLocation(0,0);
add(myLabel);
add(FileLocation);
add(Browse);
add(Go);
add(Compress);
Browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rVal =c.showOpenDialog(null);
if(rVal == JFileChooser.APPROVE_OPTION) {
filename=c.getSelectedFile().getAbsolutePath();
Location=c.getSelectedFile().getParent();
FileLocation.setText(filename);
try {
ReadH=new BufferedReader(new FileReader(filename));
} catch (IOException ex) {
JOptionPane.showConfirmDialog(null,"Cannot Read The File ?"
,"Error",JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
if(rVal == JFileChooser.CANCEL_OPTION) {
FileLocation.setText("");
filename=null;
}
}
});
Go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FillArea();
}
});
Compress.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
create_Diectionary();
CompressFunction();
}
});
}
public void CompressFunction() {
if(filename!=null) {
String seq=null;
short Code;
short Old_Code;
short result;
try {
file = new File(Location+"/Com.txt");
file_output = new FileOutputStream(file);
data_out = new DataOutputStream(file_output);
} catch (IOException ex) {
ex.printStackTrace();
}
area.setText("Reading Data From The File and Compersing it ...!!!"+'\n');
try {
ReadH=new BufferedReader(new FileReader(filename));
Old_Code=Code=(short)ReadH.read();
seq=Character.toString((char)Code);
while(seq!=null) {
if(seq.length()>1)//One Character Only
{
result=search_in_Diectionary(seq);
if(result==-1) {
addToDiectionary(seq);
seq=Character.toString(seq.charAt(seq.length()-1));
area.append(Short.toString(Old_Code)+" ");//
data_out.writeShort(Old_Code);
Old_Code=(short)((char)seq.charAt(0));
} else {
Old_Code=result;
}
}
Code=(short)ReadH.read();
seq+=Character.toString((char)Code);
if(Code==-1)//end of file
{
area.append(Short.toString(Old_Code)+" ");//
data_out.writeShort(Old_Code);
area.append('\n'+"File Compressed ..."+'\n'+"Location: "+Location+"/Com.txt");//
area.append('\n'+"Thank you for using this program");//
break;
}
}
ReadH.close();
data_out.flush();
data_out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void FillArea() {
try {
area.setText(" ");
String line=ReadH.readLine();
while(line!=null) {
area.append(line+'\n');
line=ReadH.readLine();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void create_Diectionary() {
diectionary=new ArrayList();
for(short i=0;i<256;i++) {
diectionary.add((char)i);
}
}
public short search_in_Diectionary(String seq) {
for(short i=256;i<diectionary.size();i++) {
if(seq.equals((String)(diectionary.get(i)))) {
return i;
}
}
return -1;
}
public void addToDiectionary(String seq) {
diectionary.add(seq);
}
private JFileChooser c = new JFileChooser();
private JLabel myLabel=new JLabel("File Location: ");
private JTextField FileLocation=new JTextField(20);
private JButton Browse=new JButton("/..");
private JButton Go=new JButton("Go");
private JButton Compress=new JButton("Compress");
private String filename;
private String Location;
private ArrayList diectionary ;
private JTextArea area;
private BufferedReader ReadH;
private FileOutputStream file_output ;
private File file ;
private DataOutputStream data_out;
}
package lzw_copmersion.LZW_frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class decomPanel extends JPanel {
decomPanel(JTextArea area) {
this.area=area;
setSize(550,50);
setLocation(0,50);
add(myLabel);
add(FileLocation);
add(Browse);
add(Go);
add(decompress);
Browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rVal =c.showOpenDialog(null);
if(rVal == JFileChooser.APPROVE_OPTION) {
filename=c.getSelectedFile().getAbsolutePath();
Location=c.getSelectedFile().getParent();
FileLocation.setText( filename);
try {
file=new File(filename);
file_input = new FileInputStream(file);
data_in = new DataInputStream(file_input );
} catch (IOException ex) {
JOptionPane.showConfirmDialog(null,"Cannot Read The File ?"
,"Error",JOptionPane.ERROR_MESSAGE);
}
}
if(rVal == JFileChooser.CANCEL_OPTION) {
FileLocation.setText("");
filename=null;
}
}
}
);
Go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(filename!=null) {
FillArea();
}
}
}
);
decompress.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DecompressFunction();
}
});
}
public void DecompressFunction() {
if(filename!=null) {
String seq = null,nextseq = null,todic=null;
create_Diectionary();
short Code;
short Old_Code;
short result;
try {
WriteH=new BufferedWriter(new FileWriter(Location+"/Decom.txt"));
} catch (IOException ex) {
ex.printStackTrace();
}
area.setText("Reading Data From The File and Decompersing it ...!!!"+'\n');
try {
Code= data_in.readShort();
seq=Character.toString((char)Code);
while(true) {
Code=data_in.readShort();
nextseq=getTheString(Code);
result=search_in_Diectionary(seq+nextseq);
if(result!=-1) {
seq+=nextseq;
todic=null;
} else {
if(!nextseq.equals("?")) {
todic=seq+nextseq.substring(0,1);
addToDiectionary(todic);
WriteH.write(seq);
seq=nextseq;
} else {
todic=seq+seq.substring(0,1);
addToDiectionary(todic);
WriteH.write(seq);
seq=nextseq=todic;
}
}
}
} catch (IOException ex) {
try {
if(seq!=null) {
WriteH.write(seq);
area.append('\n'+"The File Decompressed... "+'\n');
area.append("Location : "+Location+"/Decom.txt"+'\n');
}
} catch (IOException ex1) {
ex.printStackTrace();
}
}
try {
WriteH.flush();
WriteH.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public String getTheString(short code) {
if(code<256)
return Character.toString((char)code);
else if(code<diectionary.size()) {
return (String)(diectionary.get(code));
} else return "?";
}
public void create_Diectionary() {
diectionary=new ArrayList();
for(short i=0;i<256;i++) {
diectionary.add((char)i);
}
}
public short search_in_Diectionary(String seq) {
for(short i=256;i<diectionary.size();i++) {
if(seq.equals((String)(diectionary.get(i)))) {
return i;
}
}
return -1;
}
public void addToDiectionary(String seq) {
System.out.println(seq);
diectionary.add(seq);
}
public void FillArea() {
if(filename!=null) {
try {
area.setText(" ");
if(data_in==null)
System.out.println("NULL");
short ch=data_in.readShort();
while(ch!=-1) {
area.append(Short.toString(ch)+" ");
ch=data_in.readShort();
}
} catch (IOException ex) {
}
}
}
private JFileChooser c = new JFileChooser();
private JLabel myLabel=new JLabel("File Location: ");
private JTextField FileLocation=new JTextField(20);
private JButton Browse=new JButton("/..");
private JButton Go=new JButton("Go");
private JButton decompress=new JButton("Decompress");
private String filename;
private JTextArea area;
private ArrayList diectionary ;
private BufferedWriter WriteH;
private String Location;
private File file ;
private FileInputStream file_input;
private DataInputStream data_in ;
}
package lzw_copmersion.LZW_frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class myframe extends JFrame{
/** Creates a new instance of myframe */
public myframe()
{
setSize(550,450);
setLocation(200,200);
setTitle("LZW-Compersion");
JSTextArea.setLocation(10,120);
JSTextArea.setSize(520,190);
FileData.setLocation(10,100);
FileData.setSize(100,20);
Container cp=getContentPane();
cp.setLayout(null);
cp.add(comP);
cp.add(decomP);
cp.add(FileData);
cp.add(JSTextArea);
cp.add(credit_text);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private JTextArea text=new JTextArea(50,20);
private JLabel FileData=new JLabel("File Data :");
private JScrollPane JSTextArea=new JScrollPane(text);
private comPanel comP=new comPanel(text);
private decomPanel decomP=new decomPanel(text);
private CreditArea credit_text=new CreditArea();
}
class CreditArea extends JTextArea
{
CreditArea()
{
setSize(500,90);
setLocation(10,320);
setFont(new Font("Tahoma",Font.BOLD,12));
setForeground(Color.RED);
append(credits[0]+'\n');
setBorder(BorderFactory.createTitledBorder("Credits"));
}
private String[] credits=new String[]{"Developed codemiles Team ",};
}
Sat Sep 08, 2007 10:53 pm
Sun Sep 09, 2007 12:06 am
Mon Sep 10, 2007 1:44 pm
Wed Sep 12, 2007 4:27 pm
blissieng wrote:hello again,
this has bee of help, but there are some aspect i dont understand fully...pls could u send something? comments or explanation what some of the algorithm method do....
it'd be great if u could help again. thnx
as well as the huffman coding.
http://en.wikipedia.org/wiki/LZW
http://marknelson.us/1989/10/01/lzw-data-compression/
http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci214337,00.html
http://www.geocities.com/yccheok/lzw/lzw.html
http://www.cs.cf.ac.uk/Dave/Multimedia/node214.html
http://www.data-compression.com/lossless.html
http://en.wikipedia.org/wiki/Huffman_coding
http://www.compressconsult.com/huffman/
http://www.eee.bham.ac.uk/WoolleySI/All7/huff_1.htm
http://www.si.umich.edu/Classes/540/Readings/Encoding%20-%20Huffman%20Coding.htm
http://www.nist.gov/dads/HTML/huffmanCoding.html
http://www.cs.cf.ac.uk/Dave/Multimedia/node210.html
http://www.cs.duke.edu/csed/poop/huff/info/
http://www.cs.sfu.ca/CC/365/li/squeeze/Huffman.html
http://www.sbg.ac.at/geo/agit/papers96/skupin.htm
Mon Jan 21, 2013 1:36 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.