Total members 11894 |It is currently Sun Nov 24, 2024 4:39 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This is java J2SE program for LZW compression-decompression algorithm implementation using java language that let the user to browse for the file that need to compress or decompress and then make the required operation by LZW technique.

1. Run the program .
2. If you have a text and you want to compress click on Compress button and then select text file. try the enclosed file at.txt.
3. The compressed file (com.txt) will be created.
3. if you want to decompress the compressed file click on Decompress button and select com.txt file.
4. The decompressed file (decom.txt) will be created.

Hi ,
THis is the project java files ,,,
java code
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();
}

}

java code
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;



}

java code
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 ;

}

java code
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 ",};
}




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time

i can't seem to find the source code to test this, and i really really need this....any help or suggestions?


Author:
Newbie
User avatar Posts: 8
Have thanks: 0 time

THANKS a bunch


Author:
Newbie
User avatar Posts: 8
Have thanks: 0 time

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.


Author:
Newbie
User avatar Posts: 8
Have thanks: 0 time

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.


These are a well know algorithms for loss -less compression of the data ,That may applications like WINZIP or WINRAR use them or advanced of them ,,,
Just Search for them on GOOGLE or YAHOO ,and you will find many many articles about them :n_cool:
Links of LZW
Code:
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

Links of Huffman
Code:
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


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 6 posts ] 

  Related Posts  to : LZW data compression-decompression algorithm java code
 Huffman Data compression-decompression code C#-csharp     -  
 Arithmetic data comparison and decompression java code     -  
 Huffman Compression-Decompression     -  
 Arithmatic Coding Com-Decompression algorithm     -  
 apriori algorithm java code     -  
 java code for decision tree algorithm     -  
 quicksort algorithm implementation java code- array sorting     -  
 Bubble Sort Algorithm Java Implementation Code-Sorting Array     -  
 Data set for ID3 algorithm     -  
 charm algorithm in data mining     -  



Topic Tags

Java Projects
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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